Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NSImage from CGImageRef

I am trying process an image in CoreGraphics and then return the processed image back to an NSImage for saving and displaying. I have ample resources on how to perform these functions in iOS but the helper methods seem to be missing in NSImage. In iOS the class method is imageWithCGImage:, how can you do this in Mac OS?

like image 480
MobileOverlord Avatar asked Feb 01 '12 15:02

MobileOverlord


1 Answers

The matching method in NSImage is initWithCGImage:size:.

The second argument takes the image's size in points. The factor between the size in pixels (of the CGImage) and the size in points is the scale factor. So, for example, if you have a 100×100px CGImage, and pass a size of (NSSize){ 50.0, 50.0 }, the image will be 50 points in size, and double-resolution.

Usually you should just pass the size in pixels (from the CGImage) as the size in points. For handling multiple scale factors, it's better to use a single NSImage with multiple NSImageReps, like what you get from -[NSWorkspace iconForFileType:] for most types or from creating an NSImage from a typical .icns file.

like image 112
Peter Hosey Avatar answered Sep 19 '22 08:09

Peter Hosey