• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Relative pathnames e.g. when initializing NSImages?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Relative pathnames e.g. when initializing NSImages?


  • Subject: Re: Relative pathnames e.g. when initializing NSImages?
  • From: Brock Brandenberg <email@hidden>
  • Date: Sat, 11 May 2002 19:38:28 -0500

Hi Nathan.

Andrew is right when he states:

The current path gets set to / when the app is launched the Finder.

So, the trick is to establish the location of your app at runtime. To do so, use the NSBundle methods to locate the path to the app bundle, then concatenate the location of the folder where you're storing the graphics images. [[NSBundle mainBundle] bundlePath] will give you the path to the application bundle. You can then use the NSString methods to strip off the part of the path that specifies the application bundle and to concatenate the new folder and file name. The two NSString methods listed below make this easy, taking into account the presence or lack of the "/" for folders like the root folder. This way, you don't have to do the nasty string parsing yourself. For example:

NSImage *image;
NSString *imageFilePath = [[NSMutableString alloc] initWithString:@""];

[imageFilePath setString:[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent]];
[imageFilePath setString:[imageFilePath stringByAppendingPathComponent:@"Graphics/image.tiff"]];
image = [[NSImage alloc] initWithContentsOfFile:imageFilePath];

FYI, if you want the location of the Home directory for a user, use the Foundation function:

NSString *NSHomeDirectory(void)

Hope this helps.

Brock Brandenberg

----- industrial design @ bergdesign.com ------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Adding AS Studio functionality to existing Cocoa app?
  • Next by Date: Basic OpenGL 3d drawing
  • Previous by thread: Re: Displaying a menu at arbitrary location.
  • Next by thread: [ANN] Macinbench help needed
  • Index(es):
    • Date
    • Thread