copy bundle resour..." /> copy bundle resour..." /> copy bundle resour..."/>

Getting a nil path from NSBundle

5.1k views Asked by At

I have created a new folder in my project in which I have copied an image (called "io.jpg").
I also have checked on build phases -> copy bundle resources and the file is there.
So I am trying to get the path of this image:

NSBundle* bundle=[NSBundle mainBundle];
NSString* path=[bundle pathForResource: @"io" ofType: @"jpg"];
NSLog(@"%@",path);

But it prints (null), I also have tried this way:

NSBundle* bundle=[NSBundle mainBundle];
NSString* path=[bundle pathForImageResource: @"io"];
NSLog(@"%@",path);

But it still prints (null).
What's the problem?

6

There are 6 answers

0
Jessedc On BEST ANSWER

My guess, given that you said you created a new folder in your Xcode project is that you have created a blue folder reference and your image resource is in a sub directory of your bundle.

I'd be willing to bet it's not a bug with NSBundle, given how old and crutial the class is to the Foundation framework.

Try and access your resource using the more specific instance method

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension inDirectory:(NSString *)subpath

where subpath is the name of the folder reference I am guessing you created.

0
seenu On

Go to : Target -> "Build Phases" -> "copy bundle Resources" Then add that particular file here.

clean the project and RUN. It works. :)

2
Yep_It's_Me On

I had a problem like this a few weeks ago. And it turned out I just hadn't ticked a box. Here is the answer I got when I asked.

"Select the file on in the Xcode Project Navigator (to the left) and make sure that your target is checked under "Target Membership" in the File Inspector (to the right). -- Also double-check the spelling of the file names – Mundi"

4
Steven Fisher On

You'll get a nil path if the resource you're requesting doesn't exist in the output (or doesn't exist where it should). That's really the only reason I've seen.

Forget about proving it should exist, and just check the output to make sure it does.

(Remember also that filenames are case sensitive.)

To clarify, you should be looking in the output bundle in ~/Library/Developer/Xcode/DerviedData/Project-{GUID}/Build/Products. Your image will be missing.

2
Alex Gray On

All of this is kind of overkill if you're just trying to get an image. Let's say you have included bundleImage.PNG in your application bundle.. somewhere, somehow…

NSImage *image = [NSImage imageNamed:@"bundleImage"];

will, for sure.. find it… If it's there… Can't get more simple, right?

If - in the off chance - it's a slightly more complicated situation, like the image is in a loadable bundle or framework - you can use code similar to the following (a class category on NSImage) which will also return the resource.

+ (id) imageInFrameworkWithFileName:(NSString *) fileName {
   NSBundle *b = [NSBundle bundleForClass: [DummyClass class]];
   return [self imageWithFileName: fileName inBundle: b];
}

To be honest, I don't really understand the concept of, or how such a "Dummy class" works.. but this is what it looks like

@interface DummyClass : NSObject
@end
@implementation DummyClass 
@end
0
AJS On

Try removing the image from your app.And then adding it again.And clean your build before running it.See if it helps.Otherwise please give a screenshot of your project navigator showing where is the image added.It must be in the top level of your Application.app.Is it somwhere inside any folder/sub-folder.