thealch3m1st
thealch3m1st

Reputation: 282

Loading resources from another bundle

If I have a bundle that contains a class and some resources used by that class. If I load the class from the bundle how should I load the resources(that are in the bundle where I loaded the class from) in that class?

Let's say I want to load an image inside my object instantiated from the class loaded from the bundle. If I do

NSImage *image = [NSImage imageNamed:@"myImage"];

Will it load the image that's inside the bundle from where I loaded the class from? or will it look in the bundle of the application that loaded the bundle with the class and resources?

Upvotes: 6

Views: 4269

Answers (3)

strange
strange

Reputation: 9724

On OS X 10.7+ do this do this:

NSBundle *otherBundle = [NSBundle bundleWithIdentifier: @"com.company.otherapp"];
NSImage *imageFromOtherBundle = [otherBundle imageForResource: @"imageName"];

Upvotes: 16

Rob Keniger
Rob Keniger

Reputation: 46028

You can use the +bundleForClass: class method of NSBundle to get a reference to the bundle for a particular class.

You can then use the ‑pathForImageResource: method of NSBundle to get the path to the image and create an NSImage with that path.

Upvotes: 0

Alexsander Akers
Alexsander Akers

Reputation: 16024

This method searches for named images in several places, returning the first image it finds matching the given name. The order of the search is as follows:

  1. Search for an object whose name was set explicitly using the setName: method and currently resides in the image cache.
  2. Search the application's main bundle for a file whose name matches the specified string. (For information on how the bundle is searched, see ““Accessing a Bundle's Contents”“ in Bundle Programming Guide.)
  3. Search the Application Kit framework for a shared image with the specified name. When looking for files in the application bundle, it is better (but not required) to include the filename extension in the name parameter. (Link)

Upvotes: 4

Related Questions