Getting the null path from NSBundle

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

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

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

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

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

+3
source share
6 answers

My guess, given that you said you created a new folder in your Xcode project, is that you created a link to the blue folder, and the image resource is in a subdirectory of your package.

I bet it's not a mistake with the NSBundle, considering how old and crutial the class is with the Foundation Foundation.

Try and access the resource using a more specific instance method

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

where subpath is the name of the folder link. I assume you created.

+3
source

Go to: Target β†’ "Build Phases" β†’ "copy bundle Resources" Then add this specific file here.

clear project and run. It is working.:)

+5
source

You will get a null path if the requested resource does not exist at the output (or does not exist where it should). This is really the only reason I have seen.

Forget it should exist, and just check the output to see if it does.

(Remember also that file names are case sensitive.)

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

+3
source

I had such a problem a few weeks ago. And it turned out I just did not notice the check mark. Here is the answer I received when I asked.

"Select the file in the Xcode Project Navigator (left) and make sure your target is marked in the" Target Membership "in the" File Inspector "(right). - Also check the spelling of the file names - Mundi"

0
source

All this is redundant if you are just trying to get an image. Say you included bundleImage.PNG in your application package .. somewhere, somehow ...

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

for sure .. will find him ... If he is there ... It couldn't be easier, right?

If - on time - this is a bit more complicated situation, for example, the image is in a downloadable package or structure - you can use code similar to the following (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]; } 

Honestly, I really don't understand the concept or how such a "Dummy" class works, but it looks like this:

 @interface DummyClass : NSObject @end @implementation DummyClass @end 
0
source

Try removing the image from your application. Then add it again. And clean your build before running. See if that helps. Otherwise, please give a screenshot of your project navigator showing where the image is added. It should be at the top level of your Application.app.Is it somewhere inside any folder / subfolder.

0
source

Source: https://habr.com/ru/post/1480023/


All Articles