5

My QuickLook plugin generates HMTL preview for the document. I need to display images saved in the plugin bundle. Simply using imageNamed: method to get an instance of the NSImage class doesn't work. How can achieve that? Is that a consequence of the fact that

Quick Look generators are designed as CFPlugIn-style bundles.

as the documentation says?

dzolanta
  • 641
  • 7
  • 10

2 Answers2

8

I believe +imageNamed: uses the +mainBundle method of NSBundle. In that case, that's not your plugin's bundle.

I think you'll need to ask the plugin's bundle directly:

NSString * path = [[[NSBundle bundleForClass:[MyPluginClass class]] pathForResource:@"MyImage" ofType:@"tif"];
NSImage * image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];

This was written in the browser, so it may not be exact. :-)

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • 1
    Thank you for your response. It works and turns out to be very helpful. Just to clarify things up I would just like to add for anyone who may come across this thread that previously mentioned MyPluginClass class does not have to be one's class written specifically for implementing actual Quicklook plugin. It just has to be included in Quicklook plugin bundle. – dzolanta Nov 27 '10 at 20:47
  • Note: -autorelease call must be removed for use with ARC. – Joshua Nozzi Mar 02 '13 at 22:47
5

Within the plug-in code, you can access your plug-in CFBundle:

QLThumbnailRequestGetGeneratorBundle or QLPreviewRequestGetGeneratorBundle

Once you have the bundle, you can query for resources file URLs using:

CFBundleCopyResourceURL

Julien
  • 3,427
  • 20
  • 22