How to develop an iOS infrastructure that includes image resources?

I am developing an iOS infrastructure that includes image resources, I call the methods below in the framework,

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

and then I create a demo to test my frameworks, but find crossImage and arrowImage are zero; Subsequently, I compute the imageNamed: method imageNamed: will search for images in the application directory, and not in the framework, so I can fix this by adding two images to the demo project. However, he is barely elegant. why any other image targeting solutions in my framework?

+6
source share
4 answers

Finally, I create a package to store all image resources, and we provide * .framework and * .bunlde, which is more elegant.

0
source

You can download the image from the framework using:

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

method.

In your class class write as:

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

See the UIImage Class Reference for more information on this method.

+20
source

You can also upload images using:

 [[UIImage alloc] initWithContentsOfFile:path]; 

The path you want to create from the bundle of bundles. Sort of:

 NSBundle *bundle = [NSBundle bundleForClass:[YourFramework class]]; NSString* path = [bundle pathForResource:@"cross.jpg" ofType:nil]; 
+1
source

This will only work when your images are NOT in .xcasserts .

 UIImage *image = [UIImage imageNamed:@"YourFramework.framework/imageName"] 
+1
source

Source: https://habr.com/ru/post/983140/


All Articles