Dan Wood: The Eponymous Weblog (Archives)

Dan Wood Dan Wood is co-owner of Karelia Software, creating programs for the Macintosh computer. He is the father of two kids, lives in the Bay Area of California USA, and prefers bicycles to cars. This site is his older weblog, which mostly covers geeky topics like Macs and Mac Programming. Go visit the current blog here.

Useful Tidbits and Egotistical Musings from Dan Wood

Categories: Business · Mac OS X · Cocoa Programming · General · All Categories

Wed, 07 Feb 2007

CGImageRef in an NSImage

I recently blogged the praises of CGImageRep. David Catmull suggested in the comments that I make an NSImageRep subclass that draws a CGImageRef, so I can continue to use my NSImage-based code.

Well, it turns out that there is a private class in Cocoa, NSCGImageRep. It's exactly what the doctor ordered. There's a class-dump of its functions here; it's very straightforward.

I put in some code to check that this class exists and responds to the initWithCGImage: method. If the class is available, my code constructs the image by creating and adding this representation instead of performing the conversion I mentioned earlier. It's much cleaner and I assume it will be faster.

It's annoying that NSCGImageRep is a private class. I do hope that Apple will see how useful this class will be do developers and make it part of Leopard. I've filed my bug report (4981955) and I hope you will file a bug report too!

Update: Anybody wishing to file a "me-too" bug report can reference the original report, which is 3880068.