iOS 如何把图片资源打包成bundle文件及遇到的坑(详解)
1. 创建bundle 如图所示:

找到如图所示:

输入所创建的图片库名称

2. 点击创建好的bundle ,修改属性
(1.)"Base SDK" 设置为 "Latest iOS (iOS 11.2)" (Xcode 9.2为例)
"Build Active Architecture Only" 设置为 "YES"

(2.)Installation Directiotory 删除掉后面的路径

(3.)Code Signing Identity 选择 Don't Code Sign

(4.)"iOS Deployment Target" 设置为 iOS 8.0 (为了兼容性,最好选择最低版本)

(5.)"Skip Install" 设置为 "NO"

(6.)"Strip Debug Symbols During Copy" 中"Release"模式设置为 "YES"

(7.) "COMBINE_HIDPI_IMAGES" 设置为 "NO"

3. 现在开始导入图片资源(1.直接拖项目中,系统会自动导入Copy Bundle Resources里去,2.如下图加进去)
4. 选择创建的bundle 进行编译,开始生成bundle,分别选择真机和模拟器,然后各运行一遍,即可生成真机和模拟器使用的bundle:
5. 找到生成的bundle,打包上架APP的时候应使用真机模式下运行生成的Bundle,即Debug-iPhoneos 文件夹内的bundle。

6.bundle的使用,因为bundle是静态的,不进行编译的资源文件。所以,要使用bundle中的资源,就需要找到相应的资源路径。
(第一种方法)
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
UIImage *image = [UIImage imageNamed:@"Image.bundle/Add.png"];
[imgView setImage:image];
(第二种方法)
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
NSString *bundlePath = [[ NSBundle mainBundle] pathForResource:@"Image" ofType :@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *imgPath = [bundle pathForResource:Add ofType:@"png"];
UIImage *image=[UIImage imageWithContentsOfFile: imgPath];
[imgView setImage:image];
注:打包的bundle给别人使用,别人在打包完上传过程中可能会极大的坑
因为是和SDK一起让别的公司共用的,没想到每个App打包上传都失败了,主要有以下几种提示:

有很多解决办法,我用的是最简单的办法:就是删除bundle里的执行文件:找到工程中的Image.Bundle,右键单击后 选择 "显示包内容",找到里面的info.plist文件 ,删除掉Executable file 字段,重新打包,上传应用商店就可以了。