前言
在Framwork中包含了本地图片的读取和使用时,直接使用[UIImage imageNamed:...]是没有办法找到对应的图片的;
而在Framwork中 打包图片的方式,也有多种
一、使用Bundle
- 创建一个
Bundle,把图片资源放入Bundle中;
image.png
- 把该
Bundle与Framework同时导入主工程中;
- 使用该
Bundle中的图片资源
NSString *path = [[NSBundle mainBundle] pathForResource:@"Source" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
return [UIImage imageNamed:@"test" inBundle:bundle compatibleWithTraitCollection:nil];
不论
Framework是静态库或者动态库,都可以使用此种方式引入图片资源;
二、使用xcassets
我们可以在Framework内部,新建一个xcassets文件,把图片引入xcassets中;
该
xcassets文件,在编译成功后,会编译为Assets.car文件
- 使用
xcassets中的图片
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
return [UIImage imageNamed:@"test" inBundle:bundle compatibleWithTraitCollection:nil];
使用
xcassets方式,Framework必须为动态库,静态库图片资源获取不到;
三、直接引入
-
可以把图片直接引入到
Framework中;
-
Framework编译成功后
-
使用该图片
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
return [UIImage imageNamed:@"name" inBundle:bundle compatibleWithTraitCollection:nil];
此种方式,
Framework必须为动态库,静态库图片资源获取不到;









网友评论