公司的一个项目,需要做成一个.a静态库供别人调用,但是静态库中引用到了图片,于是在网上寻找解决的办法。
在iOS中,资源库是用bundle来实现的,网上的很多做法都是建立一个target,然后在target选择bundle,把图片放到bundle中,这种方法的缺点是:bundle只允许在mac中建立,而iOS是不支持的,所以要先选择mac平台建立target,然后建立bundle,再将图片拷进去,再修改配置属性来支持iOS平台,操作太繁琐了。
其实bundle本质上就是一个目录,里面可以存放各种资源,比如图片,xib等等。因此,简单的做法是:建立一个目录,把你的图片放进去,将目录重命名为xxx.bundle,然后就库在代码中读取图片资源了,比如要读取usc.bundle 中的alert.png图片:
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"usc" ofType:@"bundle"]];
NSString *alertImagePath = [bundle pathForResource:@"alert" ofType:@"png"];
UIImage *img = [UIImage p_w_picpathWithContentsOfFile:alertImagePath];
这样就可以将.bundle 和 .a 文件一起发布出去了。
转载于:https://blog.51cto.com/ikinglai/1079730