在构建Framework或者是Library的过程中,我们难免会使用到一些图片资源或者是xib文件,那如何管理这些资源文件,大家可能都知道把他们打包进bundle文件里,那么如何在framework内部或者外部使用到bundle里的资源文件呢,现在简要记录一下。
创建Bundle
1、创建Bundle
image.png2、修改Build Settings里的配置
修改Base SDK
image.png修改HIDPI
image.png将需要的资源放进Copy Bundle Resource里
image.png3、生成Bundle
build生成Bundle
image.png导出Bundle
image.pngBundle资源文件使用
Bundle里的资源文件里有图片和Xib,下面我们分两部分来分别说明如何使用
image.png1、图片
//方式一:
UIImage *image = [UIImage imageNamed:@"Resource.bundle/1.png”];
//方式二:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:path];
image = [UIImage imageNamed:@"1.png" inBundle:resourceBundle compatibleWithTraitCollection:nil];
2、xib
因为xib最后打包后会生成一个nib文件,所以在Bundle文件夹里你看到的是一个nib文件。
如果在APP项目里的bundle里面放入一个xib文件,编译器是不会发现并编译这个xib文件并将它打包到APP里面的,所以在bundle中我们只能放nib文件来代替xib文件
使用nib文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:path];
UIView *testView = [resourceBundle loadNibNamed:@"TestView" owner:nil options:nil].firstObject;
testView.frame = CGRectMake(0, 0, 200, 200);
testView.backgroundColor = [UIColor redColor];
[self.view addSubview:testView];
参考:
https://blog.cnbluebox.com/blog/2014/12/08/xib-in-frameworks/
http://www.cocoachina.com/ios/20150127/11022.html
网友评论