Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load resources from external framework

I have build an external framework, and I am trying to utilize some images which are in the resource directory of the framework. The framework is not being copied within the application but rather just referenced to it. How can I utilize UIImage accordingly from xyz.framework resource folder?

Thanks

like image 849
topgun Avatar asked Aug 01 '12 23:08

topgun


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 is NSBundle?

NSBundle(IntPtr) A constructor used when creating managed representations of unmanaged objects; Called by the runtime. NSBundle(NSObjectFlag) Constructor to call on derived classes to skip initialization and merely allocate the object.

How do I add a framework in Xcode?

To include a framework in your Xcode project, choose Project > Add to Project and select the framework directory. Alternatively, you can control-click your project group and choose Add Files > Existing Frameworks from the contextual menu.

What is bundle in Swift IOS?

The bundle object provides a single interface for locating items, taking into account the bundle structure, user preferences, available localizations, and other relevant factors. Any executable can use a bundle object to locate resources, either inside an app's bundle or in a known bundle located elsewhere.


2 Answers

What you need to do is load the bundle for the framework, and then access the resources using the NSBundle object.

For example, if there is a framework that defines a class "FrameworkClass", we can do:

NSBundle *frameworkBundle = [NSBundle bundleForClass:[FrameworkClass class]];
NSString *resourcePath = [frameworkBundle pathForResource:@"an_image" ofType:@"jpeg"];
UIImage *image = [UIImage imageWithContentsOfFile:resourcePath];

That should more or less do what you want.

like image 136
mamackenzie Avatar answered Oct 05 '22 19:10

mamackenzie


You can refer to Framework resources as follows :

[[NSBundle mainBundle] pathForResource:@"FI.framework/Resources/FileName"
                                ofType:@"fileExtension"];

Note, here FI is your framework name.

Ref. Link : http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/

like image 34
DShah Avatar answered Oct 05 '22 19:10

DShah