有的时候在项目中用到的图片不能放到Assets.xcassets中,直接拖到工程中又会使包过大,这时我们就需要将图片资源打包成bundle,接下来直接上打包的过程。(xcode 10.1)
1、

2、*如下bundle创建完毕*

3、接下来修改属性
前往build setting修改以下属性:
1> ”base sdk “ 改为 iOS(因为创建的时候是基于macOS创建的,所以要修改成iOS)

2> “Build Active Architecture Only” 设置为yes(仅编译当前环境,如果每个环境都编译一遍会使包变大)

3> "Skip Install" 设置为 NO(不需要安装相关配置)

4> “Installation Directiotory ”删掉后面的路径(不安装相关配置,也不需要安装路径)

5> "COMBINE_HIDPI_IMAGES" 设置为 "NO"(不然图片会是tiff格式)

6> "IOS Deployment Target" 设置为 "iOS 8.0"为了兼容更多版本

7>"Strip Debug Symbols During Copy" 中"Release"模式设置为 "YES"
8> Code Signing Identity 选择 Don't Code Sign
2、以上就是属性的配置,接下来就是导入图片

3、图片导入后,这个图片bundle就建立完成了,那么如何使用呢?
将项目调成在真机模式下 然后command+b编译程序

接下来选中这个bundle,show in finder,选择真机下编译出的bundle


接下来就可以把这个bundle文件拖拽到项目中去使用啦~~
3、那么具体用代码怎么使用bundle中的图片呢?直接上代码
NSString * bundlePath = [[NSBundle mainBundle]pathForResource:@"imageShare" ofType:@"bundle"]; NSBundle*bundle = [NSBundlebundleWithPath:bundlePath];
NSString* image = [bundlepathForResource:nameofType:@"png"];
如果文中有不正确的地方还希望各大网友批评指正!
网友评论