-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |