Created
July 20, 2020 02:41
-
-
Save dagronf/17c4cbe18150388ee685f13cb5b27df8 to your computer and use it in GitHub Desktop.
Swift-based ValueTransformer that converts CIImage to NSImage
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
@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