Skip to content

Instantly share code, notes, and snippets.

@dagronf
Created July 20, 2020 02:41
Show Gist options
  • Save dagronf/17c4cbe18150388ee685f13cb5b27df8 to your computer and use it in GitHub Desktop.
Save dagronf/17c4cbe18150388ee685f13cb5b27df8 to your computer and use it in GitHub Desktop.
Swift-based ValueTransformer that converts CIImage to NSImage
@objc class CIImageToNSImageTransformer: ValueTransformer {
@objc public static func RegisterTransformer() {
ValueTransformer.setValueTransformer(
CIImageToNSImageTransformer(),
forName: NSValueTransformerName("CIImageToNSImageTransformer")
)
}
override class func transformedValueClass() -> AnyClass {
return NSImage.self
}
override class func allowsReverseTransformation() -> Bool {
return false
}
override func transformedValue(_ value: Any?) -> Any? {
if let ciImage = value as? CIImage {
let rep = NSCIImageRep(ciImage: ciImage)
let updateImage = NSImage(size: rep.size)
updateImage.addRepresentation(rep)
return updateImage
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment