Search code examples
objective-cxcodensimagensbundleasset-catalog

Why can't I load an image which is located in the assets catalog?


I am trying to build a simple screensaver. The first thing I do is to load the image from the asset catalog with the method [NSImage imagedNamed:] which returns a nil value, that would mean that it can't find the image. When I try to load the image directly from the bundle with the method

[[NSBundle mainBundle] URLForImageResource:]

I also get a nil value. I don't understand why this happens. The image is located in the asset catalog and in the project settings I have set it as a bundle resource.


Solution

  • After I continued searching on the internet I found out that the screensaver is executed as some kind of plugin and by loading the main bundle I loaded the System Preference bundle which means that I am not able to get my resource by loading the main bundle, instead I have to load the bundle with bundleIdentifier or with the path where the bundle is saved. So my code looks like this now:

    NSBundle*bundle=[NSBundle bundleWithIdentifier:@"com.myname.APOD"];
    NSImage*image=[[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"appimage" ofType:@"png"]];