Matt Legend Gemmell Modesty is Lying

Mac OS X Cocoa and iPhone Development Services available at Instinctive Code.
Mac OS X Cocoa and iPhone Developer for hire

Other Pages

Categories

Posted
29 October 2007 @ 11pm

Categories
Development, Source

Tags
, , , , , , , ,

How to get a Quick Look preview as an NSImage

I wrote a quick category on NSImage allowing you to get an image containing a Quick Look preview of the file at a given path. You can specify the size, and whether the preview should be rendered as an icon (i.e. with a document border, drop-shadow, page-curl and file type/extension label superimposed). So, you can get both Finder-style preview icons, and also full-size previews like in the Quick Look panel.

If Quick Look can’t generate a preview for the specified file, you’ll be given the file’s Finder icon instead (which is how the Quick Look panel itself behaves in Leopard). Source is available on the Cocoa Source Code page, and includes a Read Me and the usual license document. Needless to say, this code requires Leopard or later.

Enjoy. :)

Update: Now much more efficient, thanks to a new Leopard-only method pointed out by Troy Stephens at Apple - thanks to Troy for that. You can also get the latest version of this code from the MacCode repository at Google Code.


5 Comments

Troy
31 October 2007 @ 5pm

Hi Matt,

Nice idea for a handy wrapper! I’ve got a performance enhancement for this that I think you’ll like: On Leopard, one can now initialize an NSBitmapImageRep to reference an existing CGImage. This is much more lightweight, as it avoids the memory and CPU costs of copying the pixel data. (The NSBitmapImageRep simply retains the CGImage, will only unpack a copy of the CGImage’s content if you ask it for its -bitmapData or, equivalently, invoke -getBitmapDataPlanes:, which most casual BitmapImageRep usage doesn’t do.)

NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; // Take advantage of NSBitmapImageRep’s -initWithCGImage: initializer, which is new in Leopard.
NSImage *newImage = nil;
if (bitmapImageRep) {
newImage = [[NSImage alloc] initWithSize:[bitmapImageRep size]];
[newImage addRepresentation:bitmapImageRep];
[bitmapImageRep release];
}

Note also that one can now ask an NSBitmapImageRep to return a CGImage created from its pixel data. (Be sure to keep the NSBitmapImageRep around for as long as the CGImage is in use, as the CGImage references the NSBitmapImageRep’s data but doesn’t retain the NSBitmapImageRep.)

This is mentioned in the Leopard Application Kit release notes. Enjoy!
-Troy


Matt Legend Gemmell
1 November 2007 @ 11am

Hi Troy,

Excellent, thanks for that. I’ll update the code sample. :)


Dieter Komendera
26 November 2007 @ 10am

Hi Matt,

I’ve written a little command line tool with the help of your NSImage category:
http://soakedandsoaped.com/articles/read/quickiee-create-preview-image-files-with-quicklook

Best
Dieter


Ken
15 April 2008 @ 7pm

Hi. Thanks for the sample code! I’m a little bummed though. It looks like this doesn’t actually create the same image that the Finder uses to show the QuickLook panel.

The “thumbnail” is never more than one page high. If you use this with a 10 page Word doc, for example, you’ll only get the first page, no matter what you specify for maximum height.

It seems that, internally, QL generators differentiate between thumbnails and previews, and we’re not given a public API for accessing the latter.

-Ken


Matt Legend Gemmell
15 April 2008 @ 8pm

Hi Ken,

That’s true - the current API is more intended for the icon preview rather than the Quick Look floating contents view, but it’s possible to special-case certain file types and use an appropriate view to show the actual file (like a PDFView for PDFs). Not perfect, but offers a bit more than the icon.


Leave a Comment

Get rid of your code with Leopard NSColorWell bug on Leopard