如何自己打包Bundle文件以及问题解决
2018-01-22 本文已影响28人
请叫我作家
在平时的开发中,我们会写一些比较常用的库,这时候里面会有一些图片、Xib等资源,为了方便我们在其他的项目中使用,我们便可以将其打包成bundle文件。
下面老汉带你来开车
1、新建一个工程,这里我将直接用我的Demo为大家演示:

方法详细描叙:
1、新建Bundle文件:

2、由于新建的是macOS的,所以此处需要适配一下修改:只有设置了为iOS的而不是MacOS的才可以进行编译为iOS可以运行的bundle文件哦!
"Base SDK" 设置为 "Latest iOS (iOS 11.2)" (Xcode 9.2为例)

"Build Active Architecture Only" 设置为 "YES"
Installation Directiotory 删除掉后面的路径

Code Signing Identity 选择 Don't Code Sign

"iOS Deployment Target" 设置为 iOS 8.0 (为了兼容性,最好选择最低版本)
"Skip Install" 设置为 "NO"
"Strip Debug Symbols During Copy" 中"Release"模式设置为 "YES"
"IOS Deployment Target" 设置为 "iOS 8.0"
"COMBINE_HIDPI_IMAGES" 设置为 "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];
//这样获取不到图片区分不了@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];
有不清楚的大家再问啊!我要去开会了。