在平時的開發(fā)中,我們會寫一些比較常用的庫沐旨,這時候里面會有一些圖片衅金、Xib等資源噪伊,為了方便我們在其他的項目中使用,我們便可以將其打包成bundle文件氮唯。
下面老漢帶你來開車
1鉴吹、新建一個工程,這里我將直接用我的Demo為大家演示:
方法詳細描敘:
1荡陷、新建Bundle文件:
2碎赢、由于新建的是macOS的洼滚,所以此處需要適配一下修改:只有設(shè)置了為iOS的而不是MacOS的才可以進行編譯為iOS可以運行的bundle文件哦!
"Base SDK" 設(shè)置為 "Latest iOS (iOS 11.2)" (Xcode 9.2為例)
"Build Active Architecture Only" 設(shè)置為 "YES"
Installation Directiotory 刪除掉后面的路徑
Code Signing Identity 選擇 Don't Code Sign
"iOS Deployment Target" 設(shè)置為 iOS 8.0 (為了兼容性良蒸,最好選擇最低版本)
"Skip Install" 設(shè)置為 "NO"
"Strip Debug Symbols During Copy" 中"Release"模式設(shè)置為 "YES"
"IOS Deployment Target" 設(shè)置為 "iOS 8.0"
"COMBINE_HIDPI_IMAGES" 設(shè)置為 "NO"
3、加入資源:
4伍玖、進行編譯(選擇myapp分別進行真機和模擬器編譯即可)
5嫩痰、找到目標文件:
6、如何使用它:
將我們的bundle文件導入任何項目工程中窍箍,即可使用:
7串纺、如果需要給到別人用或者上架需要刪除bundle中的不必要文件丽旅。
方便大家復制代碼:
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
NSString *bundlePath = [[ NSBundle mainBundle] pathForResource:@"myapp" ofType :@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
//這樣獲取不到圖片區(qū)分不了@2 @3
// NSString *img_path = [bundle pathForResource:@"draw_pic_bg" ofType:@"png"];
//這樣就可以獲取了
NSString *img_path = [[bundle resourcePath] stringByAppendingPathComponent:@"draw_pic_bg.png"];
UIImage *image_1=[UIImage imageWithContentsOfFile:img_path];
imgView.image = image_1;
[self.view addSubview:imgView];
有不清楚的大家再問啊纺棺!我要去開會了榄笙。