Convert CIImage to NSImage

I play with a Core Image framework. As I understand it, if I have an image ( NSImage ), I first need to convert it to CIImage . I can do it.

NSImage *im1 = [[NSImage alloc] initWithContentsOfFile:imagepath]; NSRect rect1;rect1.size.width = img1.size.width; rect1.size.height = img1.size.height; CGImageRef imageRef1 = [img1 CGImageForProposedRect:&rect1 context:[NSGraphicsContext currentContext] hints:nil]; CIImage *ciimage = [CIImage imageWithCGImage:imageRef1]; 

I have a function that applies a Core Image filter to a main image ( CIImage ) that I want to test. And I want to add the output image to the window as a preview. Therefore I need NSImage. How to convert this main image back to NSImage? If I ask Google, I will not get good results.

Thank you for your help.

+9
source share
2 answers

I have not tested this, but I think this should do it:

 CIImage *ciImage = ...; NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage:ciImage]; NSImage *nsImage = [[NSImage alloc] initWithSize:rep.size]; [nsImage addRepresentation:rep]; 

In Swift:

 let ciImage = ... let rep = NSCIImageRep(ciImage: ciImage) let nsImage = NSImage(size: rep.size) nsImage.addRepresentation(rep) 
+23
source

In Swift:

 var rep: NSCIImageRep = NSCIImageRep(ciImage: gaussianBlurFilter.outputImage) var nsImage: NSImage = NSImage(size: rep.size) nsImage.addRepresentation(rep) 
+11
source

Source: https://habr.com/ru/post/948406/


All Articles