How to develop an iOS framework which includes image resources?

Gaojin Hsu picture Gaojin Hsu · Feb 28, 2015 · Viewed 7k times · Source

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?

Answer

Midhun MP picture Midhun MP · Feb 28, 2015

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.