Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rezeda

macrumors newbie
Original poster
Nov 11, 2008
10
0
Hi everyone,
I have the following problem with loading an image in a cocoa/ obj c application.
Code that works:

NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:mad:"/Users/oana/Project/pagePhoto.jpg"];

Code that doesn't work:

NSString imageName = [[NSBundle mainBundle] pathForResource:mad:"pagePhoto" ofType:mad:"jpg"];
//NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName];
//no point in calling the previus line since imageName is allways nil in my case

I have added pagePhoto.jpg to the Resources folder in xcode, and I can see it in the build directory, in the resources.

What have I missed? Why only absolute path works and I cannot load the image from resources?

Thank you for your replies!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Sounds like something else is going on. You've triple checked that the file is being copied into the Resources folder? Did you try a Clean on the project? Are you sure the extension is .jpg - it could be something else but be hidden in the Finder.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Hi everyone,
I have the following problem with loading an image in a cocoa/ obj c application.
Code that works:

NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:mad:"/Users/oana/Project/pagePhoto.jpg"];

Code that doesn't work:

NSString imageName = [[NSBundle mainBundle] pathForResource:mad:"pagePhoto" ofType:mad:"jpg"];
//NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName];
//no point in calling the previus line since imageName is allways nil in my case

I have added pagePhoto.jpg to the Resources folder in xcode, and I can see it in the build directory, in the resources.

What have I missed? Why only absolute path works and I cannot load the image from resources?

Thank you for your replies!

The absolute path looks like you are looking for the jpeg file in your source directory. That cannot work if you give the application to anyone else, because obviously they won't have your source directory. At some point in the build process that image has to be copied into the application bundle. In your project, check the "Copy Bundle Resources" build phase for your target, that is where I would expect an entry for your pagePhoto.jpg file.
 

rezeda

macrumors newbie
Original poster
Nov 11, 2008
10
0
When I added the jpeg file to Resources, it appeared also under Copy Bundle Resources. So it is sure that the file is present in the resources folder in the application bundle.
The thing is this line:
NSString imageName = [[NSBundle mainBundle] pathForResource:mad:"pagePhoto" ofType:mad:"jpg"];
still doesn't work!

I have tried the exact code in a new application and it works.
I am currently developing a PDE (printer driver extension) and I have started from OutputBins2PDE sample code from apple.
I cannot figure it out why in this sample project the jpeg file cannot be found in resources and in a new application it works fine.

Thanks again for the support!
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
When I added the jpeg file to Resources, it appeared also under Copy Bundle Resources. So it is sure that the file is present in the resources folder in the application bundle.

Not at all. Since your code doesn't work, something is clearly not the way you think it is. Does the file appear under "Copy Bundle Resources" now? Did you check it? When you open the package, is the file where you think it would be? Don't assume, check it. What do the NSBundle methods bundlePath and resourcePath return?
 

rezeda

macrumors newbie
Original poster
Nov 11, 2008
10
0
Thank you for the help!
I run bundlePath and resourcePath and they pointed to the TextEdit bundle instead of my bundle, since the plugin I'm making is not the mainBundle!
My code works fine now!
Things are also clear in my head now!
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
Yeah for plugins you can easily do:

Code:
[[NSBundle bundleForClass:[self class]]

Instead of mainBundle to get the bundle for the plugin itself.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.