Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Relative file paths and NSImage

Relative file paths and NSImage
Thread Tools
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Feb 12, 2001, 03:25 PM
 
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
Reply With Quote
Feb 12, 2001, 05:40 PM
 
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
Reply With Quote
Feb 12, 2001, 06:57 PM
 
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.
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Feb 12, 2001, 07:56 PM
 
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

     
Dalgo  (op)
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Feb 13, 2001, 04:27 AM
 
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
Reply With Quote
Mar 3, 2001, 04:00 PM
 
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
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Mar 3, 2001, 04:12 PM
 
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
Reply With Quote
Mar 4, 2001, 07:23 AM
 
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)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2