Search code examples
macoscocoascreensaver

MacOsX: ScreenSaver -- how to get the path to .saver bundle


I have a ScreenSaver project. In the Resources of this project presents the image file. I'm trying to load it like this:

NSString* imageName = [[NSBundle mainBundle] pathForResource:@"DefaultImage" ofType:@"jpg"];
mpCurrentImage = [[NSImage alloc] initWithContentsOfFile:imageName];

But the path of mainBundle is /Applications/System Preferences.app/Contents/Resources

How to get the path to the .saver bundle, to load the image from recourses?


Solution

  • You'd want to use

    NSString* imageName = [[NSBundle bundleForClass:[self class]] pathForResource:@"DefaultImage" ofType:@"jpg"];
    

    assuming that this code is in a class belonging to the bundle.