iOS_SDK开发之Bundle打包

我们的项目中难免会用到图片资源和xib,storyboatd资源,我们可以将这些资源全部归类到bundle文件中,便于管理。
代码传送门

1.创建Bundle资源包

  • 1.1新建一个Project

    image

    选择BUndle,选择第一步创建的WorkSpace(和SDK开发一中的图1的选择一样),这样就创建好了Bundle资源工程

    image

    成功之后的示例

    image
  • 1.2 添加资源:图片和xib

    image
  • 1.3 修改配置

    点击Build Setting

    修改 Base SDKiOS 否则编译无法通过

    修改 COMBINE_HIDPI_IMAGESNo 否则Bundle中的图片就是tff格式

    删除安装路径 Installation Directory 的值。作为资源包,不需要安装

    image
    image
    image
  • 1.4 编译后去.bundle资源包

    编译运行获取到.bundle

2.将产生的资源和SDK的集成Demo进行关联

  • 编译运行后,在Products文件找到.bundle包 右击选择 Show in Finder,找到Debug-iphoneos文件中.bundle拖到SDK的集成Demo中。

    image
    image
    image
  • 使用bundle资源包,并运行。

    获取资源代码

    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"LWTestBundle" ofType:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
    // VC的nib资源
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"LWTestBundle" ofType:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
    UIView *view = [[resourceBundle loadNibNamed:@"LWBundleView" owner:self options:nil]objectAtIndex:0];
    [self.view addSubview:view];
    // 图片资源
    UIImage *img1 = [UIImage imageNamed:@"gaitubao_timg_png" inBundle:resourceBundle compatibleWithTraitCollection:nil];
    ///或者使用
    //UIImage *img2 = [UIImage imageNamed:[resourceBundle pathForResource:@"gaitubao_timg_png" ofType:@"png"]];
    ///或者
    //NSString *imgPath= [bundlePath stringByAppendingPathComponent:@"gaitubao_timg_png.png"];
    //UIImage *image_1=[UIImage imageWithContentsOfFile:imgPath];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 144, self.view.frame.size.width, self.view.frame.size.height - 144)];
    ///图片资源加载
    imgView.image = img1;
    [self.view addSubview:imgView];
    
    
    
    image
    image
  • 运行结果如下

image

image

最后的最后,把这个工程上传到Git就可以愉快的开发了。。。以上是我个人的理解,如有错误,请各位大牛批评指正,文中bundle的打包方法参考网上资料
参考链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。