Search code examples
iphoneobjective-ciosnsbundle

NSBundle -- finding resources within a bundle?


I created a directory MyDirectory inside a bundle myBundle. I then put an image myImage.png inside MyDirectory. But a call to

 [myBundle pathForResource: @"myImage" ofType: @"png"] 

does not find the image. What am I missing?

EDIT: To clarify, if the image is in the top level of the bundle, it finds it just fine.


Solution

  • First you must have added the image to the bundle. If you've did it, I guess you would have added folder references while you added the image to the project. If so, you should specify the folder hierarchy with the image name.

    [myBundle pathForResource:@"MyDirectory/myImage" ofType:@"png"];
    

    Edit: As Deepak commented below, there is even a better method pathForResource:ofType:inDirectory:

    [myBundle pathForResource:@"myImage" ofType:@"png" inDirectory:@"MyDirectory"];