Search code examples
xcodemacospluginsscreensaver

Xcode plugin load resources


I am writing a screensaver for OSX using Xcode 7.3. This screensaver will show an image of a ball bouncing around the screen. However, I am currently unable to load the image from the bundle.

This is how I load the image in the ScreenSaverView: NSImage *img = [NSImage imageNamed:"ball.png"]; and img turns out to be nil.

The image is definitely added to the bundle: it is added to "Copy Bundle Resources" for the screensaver target; it is in Ball.saver/Contents/Resources.

I tried to print out the resource path with [[NSBundle mainBundle] resourcePath] and it prints out the path of the System Preferences /Applications/System Preferences.app/Contents/Resources, which is understandable since the screensaver is only a system plugin. My question is, how do I load the resources from the screensaver's bundle?


Solution

  • Turns out, for plugins, you have to find the particular bundle of the plug in and not just use the default bundle, which seems to be the main app's bundle. This is the final solution:

    NSBundle *bundle = [NSBundle bundleWithIdentifier:bundleId]
    NSString *filePath = [bundle pathForResource:imageName ofType:imageType];
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:filePath];