在構建Framework或者是Library的過程中,我們難免會使用到一些圖片資源或者是xib文件,那如何管理這些資源文件,大家可能都知道把他們打包進bundle文件里,那么如何在framework內部或者外部使用到bundle里的資源文件呢,現在簡要記錄一下。
創建Bundle
1、創建Bundle
image.png
2、修改Build Settings里的配置
修改Base SDK
image.png
修改HIDPI
image.png
將需要的資源放進Copy Bundle Resource里
image.png
3、生成Bundle
build生成Bundle
image.png
導出Bundle
image.png
Bundle資源文件使用
Bundle里的資源文件里有圖片和Xib,下面我們分兩部分來分別說明如何使用
image.png
1、圖片
//方式一:
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