Bundle 编译正确姿势

作者: midmirror | 来源:发表于2017-12-08 18:03 被阅读140次

    项目中一些代码是一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];
    

    相关文章

      网友评论

        本文标题:Bundle 编译正确姿势

        本文链接:https://www.haomeiwen.com/subject/uuqyixtx.html