Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop an iOS framework which includes image resources?

I am developing an iOS framework which includes image resources, I call the methods below in the framwork,

crossImage = [UIImage imageNamed:@"cross"];
arrowImage = [UIImage imageNamed:@"arrow"];

and then I build a demo to test my framework, but find crossImage and arrowImage are both nil; Afterwards, I figure out imageNamed:method will search images in the app's directory not in the framework's, so I can fix it by adding the two images to the demo project. However, it's barely elegant. so any other solutions to target the images in my framework?

like image 954
Grey Avatar asked Feb 28 '15 03:02

Grey


People also ask

What is XC framework in iOS?

What is XCFramework? Apple defines XCFrameworks as a distributable binary package created by Xcode that contains variants of a framework or library so that it can be used on multiple platforms (iOS, macOS, tvOS, and watchOS), including Simulator builds.

What is a framework iOS?

What is a Framework? Frameworks are self-contained, reusable chunks of code and resources you can import into many apps. You can even share them across iOS, tvOS, watchOS and macOS apps. When combined with Swift's access control, frameworks help define strong, testable interfaces between code modules.

How do I create a custom framework in Swift?

In the app, select the project from the project navigator, select the Stocktance target, and scroll to Frameworks, Libraries, and Embedded Content. Click on the plus button, click Add Other… and select Add Files… Navigate to the SettingsKit folder and select it. We've added the framework to the project.

What is Uiimage?

An object that manages image data in your app.


2 Answers

You can load the image from framework using:

+ (UIImage *)imageNamed:(NSString *)name
               inBundle:(NSBundle *)bundle
compatibleWithTraitCollection:(UITraitCollection *)traitCollection

method.

In your framework class write like:

NSBundle *frameWorkBundle = [NSBundle bundleForClass:[self class]];
UIImage *arrow = [UIImage imageNamed:@"arrow" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];
UIImage *cross = [UIImage imageNamed:@"cross" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];

Refer UIImage Class Reference for more info about this method.

like image 83
Midhun MP Avatar answered Nov 12 '22 08:11

Midhun MP


You can also load images using:

[[UIImage alloc] initWithContentsOfFile:path];

The path you'll want to generate from the bundle path. Something like:

NSBundle *bundle = [NSBundle bundleForClass:[YourFramework class]];
NSString* path = [bundle pathForResource:@"cross.jpg" ofType:nil];
like image 43
DanielG Avatar answered Nov 12 '22 08:11

DanielG