Skip to content

Instantly share code, notes, and snippets.

@dagronf
Last active October 21, 2024 20:06
Show Gist options
  • Save dagronf/6d41c62a098b1e9a2f4397fd85aba207 to your computer and use it in GitHub Desktop.
Save dagronf/6d41c62a098b1e9a2f4397fd85aba207 to your computer and use it in GitHub Desktop.
Extensions for converting NSImage <-> CGImage <-> CIImage
extension NSImage {
/// Create a CIImage using the best representation available
///
/// - Returns: Converted image, or nil
func asCIImage() -> CIImage? {
if let cgImage = self.asCGImage() {
return CIImage(cgImage: cgImage)
}
return nil
}
/// Create a CGImage using the best representation of the image available in the NSImage for the image size
///
/// - Returns: Converted image, or nil
func asCGImage() -> CGImage? {
var rect = NSRect(origin: CGPoint(x: 0, y: 0), size: self.size)
return self.cgImage(forProposedRect: &rect, context: NSGraphicsContext.current, hints: nil)
}
}
extension CIImage {
/// Create a CGImage version of this image
///
/// - Returns: Converted image, or nil
func asCGImage(context: CIContext? = nil) -> CGImage? {
let ctx = context ?? CIContext(options: nil)
return ctx.createCGImage(self, from: self.extent)
}
/// Create an NSImage version of this image
/// - Parameters:
/// - pixelSize: The number of pixels in the result image. For a retina image (for example), pixelSize is double repSize
/// - repSize: The number of points in the result image
/// - Returns: Converted image, or nil
#if os(macOS)
@available(macOS 10, *)
func asNSImage(pixelsSize: CGSize? = nil, repSize: CGSize? = nil) -> NSImage? {
let rep = NSCIImageRep(ciImage: self)
if let ps = pixelsSize {
rep.pixelsWide = Int(ps.width)
rep.pixelsHigh = Int(ps.height)
}
if let rs = repSize {
rep.size = rs
}
let updateImage = NSImage(size: rep.size)
updateImage.addRepresentation(rep)
return updateImage
}
#endif
}
extension CGImage {
/// Create a CIImage version of this image
///
/// - Returns: Converted image, or nil
func asCIImage() -> CIImage {
return CIImage(cgImage: self)
}
/// Create an NSImage version of this image
///
/// - Returns: Converted image, or nil
func asNSImage() -> NSImage? {
return NSImage(cgImage: self, size: .zero)
}
}
@cgfarmer4
Copy link

Noice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment