 |
 |
Relative file paths and NSImage
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
I made a program which has a few NSImages in it. They are displayed in various ways. Anyway, when I launch the program from within project builder, using the little button that is there; the program works perfectly, however, when I doubleclick it in the finder none of my images are loaded. In my program I initialize my images like:
[[NSImage alloc] initWithContentsOfFile:@"Contents/Resources/Image.tif"];
I have found that If I use an absolute path name instead of a relative one then the icons load when they are supposed to. i can't use an absolute path because I do not know where the program will be kept. Putting ./ infront of the path did nothing. I don't understand why this will run perfectly from Project Builder but images won't load if I doubleclick it or run it under Malloc Debug.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
If it were me (since I don't know the right answer) I'd try:
1) "Contents/Resources/Image.tif"
2) "Resources/Image.tif"
3) "Image.tif"
Since you already know #1 won't work run through the others! If it doesn't work you wasted a few minutes. 
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
Use NSBundle to find resources inside the app wrapper (or other bundle directories). It does the right thing with respect to the bundle layout and localization.
[[NSBundle mainBundle] pathForResource:@"Image" ofType:@"tif"] would return the full path to the image if it's in the main app wrapper. That's the usual method used for retrieving arbitrary resources.
For images however, there is a convenience method declared in NSImage.h to look for images with common extensions, -pathForImageResource:. So, [[NSBundle mainBundle] pathForImageResource:@"Image"] should also return the correct path.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
lindberg's right on. Always use NSBundle (or CFBundle, if you're not a Cocoa app) for retrieving resources from wrappers. In addition, sometimes there will be higher level methods that use NSBundle under the covers, those are fine too.
As far as why it doesn't work with relative paths --- depending on the way you launch your app, the current working directory will be different, so relative paths might or might not work. In general, it's best to avoid relative paths altogether.
Ali
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Thanks. [[NSBundle mainBundle] pathForImageResource:@"Image"] worked. Apple should really mention this in NSImage since it says somewhere in there that it supports relative paths so I assumed that the relative path when my program launched would be the program's directory.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Feb 2001
Location: Amstelveen, Netherlands
Status:
Offline
|
|
Apologies for replying to this somewhat older thread.
I read that it is possible to use NSImage for image files within the application bundle.
My question is:
how can you load images that are in different folders on the disk then the application bundle?
For instance in the /usr/#name/ when I want to use an image that I already have on my disk.
Thanks
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Originally posted by evdmeer:
My question is:
how can you load images that are in different folders on the disk then the application bundle?
For instance in the /usr/#name/ when I want to use an image that I already have on my disk.
Thanks
That's when you would use absolute paths, for instance:
[[NSImage alloc] initWithContentsOfFile:@"/Users/Joe/SomePicture.jpg"];
Obviously it's usually not a good idea to hardwire such paths, typically you get one from the open dialog, etc.
Also note the difference between initByReferencingFile: and initWithContentsOfFile:.
The first one assumes the file is always there, as it uses the file on disk as the "backing store" for the image. (So, if the file disappears, the image might and at some point will stop displaying.) When you archive the NSImage, only the file path is saved.
The second will actually load the file into memory, so even if the file then vanishes, the image will still display properly.
Ali
[This message has been edited by ali (edited 03-03-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
To Dalgo:
What about
[NSImage imageNamed:@"my_image.tiff"]
To evdmeer:
You might try
NSString *NSHomeDirectory(void)
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|