Bundle 编译正确姿势

项目中一些代码是一framework或.a等SDK形式集成进来的,而SDK中使用到了资源文件,这时候需要把这些资源文件包括图片、xib、storyboard等打包到一个bundle中。

踩坑过程不细说了,bundle本身是一个文件夹,直接拖入到Xcode项目中,是不会自动编译里面的资源文件的。需要新建一个bundle target,同时要正确配置bundle的引入,否则bundle编译时可能不会被复制到app这个包中,导致项目中的代码获取不到。

  • 新建一个 macOS 的 bundle target,将Base SDKlast macOS改为last iOS

  • 将需要用到的资源文件包括png、xib、storyboard等复制到这个target文件夹下,并确认build phases下的compy bundle resource有正确引入这些文件

  • 在 bundle target 中Build Phases下的COMBINE_HIDPI_IMAGES设置为NO

    防止png格式被编译为tiff

  • 在 app target 中的embedded binaries中引入新建的bundele,在build phases选项卡的copy Files将Destination改为Resource。

// 获取图片
[UIImage imageNamed:[NSString stringWithFormat:@"Resources.bundle/%@",imageName]];

// 获取 storyboard
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *bundlePath = [bundle pathForResource:@"Resources" ofType:@"bundle"];
if (bundlePath) {
    bundle = [NSBundle bundleWithPath:bundlePath];
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"HLImagePicker" bundle:bundle];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容