Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 799
thealch3m1st Avatar asked Jun 25 '11 02:06

thealch3m1st


People also ask

How do I load a bundle in Swift?

It's called contentsOfFile , and here it is in action: if let filepath = Bundle. main. path(forResource: "example", ofType: "txt") { do { let contents = try String(contentsOfFile: filepath) print(contents) } catch { // contents could not be loaded } } else { // example.

What are resource bundles?

A resource bundle is a set of properties files with the same base name and a language-specific suffix. For example, if you create file_en. properties and file_de. properties, IntelliJ IDEA will recognize and combine them into a resource bundle.

What is the use of ResourceBundle?

Simply put, the ResourceBundle enables our application to load data from distinct files containing locale-specific data.

How do you load properties file from resources folder in Java?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().


2 Answers

On OS X 10.7+ do this do this:

NSBundle *otherBundle = [NSBundle bundleWithIdentifier: @"com.company.otherapp"];
NSImage *imageFromOtherBundle = [otherBundle imageForResource: @"imageName"];
like image 124
strange Avatar answered Sep 20 '22 14:09

strange


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)
like image 37
Alexsander Akers Avatar answered Sep 18 '22 14:09

Alexsander Akers