概要
Swift4でNSImage,CGImage,CIImageを相互変換するExtention
NSImgae → CGImage
extension NSImage {
public var cgImage: CGImage? {
guard let imageData = self.tiffRepresentation else { return nil }
guard let sourceData = CGImageSourceCreateWithData(imageData as CFData, nil) else { return nil }
return CGImageSourceCreateImageAtIndex(sourceData, 0, nil)
}
}
NSImage → CIImgae
extension NSImage {
public var ciImage: CIImage? {
guard let imageData = self.tiffRepresentation else { return nil }
return CIImage(data: imageData)
}
}
CGImage → NSImgae
extension CGImage {
public var nsImage: NSImage? {
let size = CGSize(width: self.width, height: self.height)
return NSImage(cgImage: self, size: size)
}
}
CGImage → CIImgae
extension CGImage {
public var ciImage: CIImage {
return CIImage(cgImage: self)
}
}
CIImage → NSImgae
extension CIImage {
public var nsImage: NSImage {
let rep = NSCIImageRep(ciImage: self)
let nsImage = NSImage(size: rep.size)
nsImage.addRepresentation(rep)
return nsImage
}
}
CIImage → CGImgae
extension CIImage {
public var cgImage: CGImage? {
let context = CIContext(options: nil)
return context.createCGImage(self, from: self.extent)
}
}