Skip to content

Commit

Permalink
Add MMMFilteredLoadableImage
Browse files Browse the repository at this point in the history
  • Loading branch information
aleh committed May 4, 2024
1 parent d0cca7c commit e711bb7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion MMMLoadable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Pod::Spec.new do |s|

s.name = "MMMLoadable"
s.version = "2.1.1"
s.version = "2.2.1"
s.summary = "A simple model for async calculations"
s.description = "#{s.summary}."
s.homepage = "https://github.com/mediamonks/#{s.name}"
Expand Down
55 changes: 55 additions & 0 deletions Sources/MMMLoadable/MMMFilteredLoadableImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Starbucks App.
// Copyright (c) 2024 MediaMonks. All rights reserved.
//

#if canImport(CoreImage)

import CoreImage

/// A simple proxy filtering the contents of another loadable image with CoreImage filters.
public final class MMMFilteredLoadableImage: MMMLoadableProxy, MMMLoadableImage {

private let upstreamImage: MMMLoadableImage
private let filters: [CIFilter]

public init(_ image: MMMLoadableImage, _ filters: [CIFilter]) {
self.upstreamImage = image
self.filters = filters
super.init()
self.loadable = image
}

public override func proxyDidChange() {
if
upstreamImage.isContentsAvailable, let uiImage = upstreamImage.image,
let ciImage = uiImage.ciImage ?? uiImage.cgImage.flatMap(CIImage.init(cgImage:))
{
// TODO: it would be great to move it out of the main thread of course.
let outputImage = filters.reduce(ciImage) { inputImage, filter in
filter.setValue(inputImage, forKey: kCIInputImageKey)
return filter.outputImage ?? inputImage
}
self.image = UIImage(ciImage: outputImage)
} else {
self.image = nil
}
}

public override var isContentsAvailable: Bool { image != nil }

public override var loadableState: MMMLoadableState {
set { self.loadableState = newValue }
get {
if super.loadableState == .didSyncSuccessfully && !isContentsAvailable {
.didFailToSync
} else {
super.loadableState
}
}
}

public private(set) var image: UIImage?
}

#endif

0 comments on commit e711bb7

Please sign in to comment.