Skip to content

Instantly share code, notes, and snippets.

@paskowski
Last active February 19, 2018 12:56
Show Gist options
  • Save paskowski/a293a45b8fcafe83b43c8170856c1fc2 to your computer and use it in GitHub Desktop.
Save paskowski/a293a45b8fcafe83b43c8170856c1fc2 to your computer and use it in GitHub Desktop.
import UIKit
class MonoImageOperation: Operation {
var inputImage: UIImage?
var outputImage: UIImage?
init(inputImage: UIImage) {
self.inputImage = inputImage
}
override public func main() {
if self.isCancelled {
return
}
outputImage = applyMonoEffectTo(image: inputImage)
}
private func applyMonoEffectTo(image: UIImage?) -> UIImage? {
guard let image = image,
let ciImage = CIImage(image: image),
let mono = CIFilter(name: "CIPhotoEffectMono",
withInputParameters: [kCIInputImageKey: ciImage])
else { return nil }
let ciContext = CIContext()
guard let monoImage = mono.outputImage,
let cgImage = ciContext.createCGImage(monoImage, from: monoImage.extent)
else { return nil }
return UIImage(cgImage: cgImage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment