views:

86

answers:

2

I am trying to make a simple that needs to create an NSImage and I want to put the .png file in the resources of my package contents. I added the .png file to my resources directory in Xcode and when I create the applicaiton the .png file shows up in my resources inside package contents but I am havhing trouble figuring out how to reference file here is what I tried:

[image initWithContentsOfFile:@"resources/draw-button.png"];

I figured that my package contents would be my current directory so I thought this would work, but it does. How do I reference my resources directory?

+2  A: 

Try [UIImage imageNamed:@"draw-button.png"].

"Resources" is just an organizational category in XCode, not a true folder in the application bundle.

Frank Schmitt
Or NSImage, in the questioner's case.
Peter Hosey
+2  A: 

You want to use NSBundle:

NSString *path = [[NSBundle mainBundle] pathForResource:@"draw-button" ofType:@"png"];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:path];
mipadi
Not necessary for images (see Frank Schmitt's answer). Also, paths must be absolute, not relative. Even if paths could be relative to the executable, "resources/..." is still incorrect, since the app bundle is not the same thing as the executable *in* the bundle. :-)
Joshua Nozzi
`-[NSBundle pathForResource:ofType]` *does* return an absolute path. (Also, `UIImage` is a Cocoa Touch class, not a Cocoa class.)
mipadi