我们的项目中难免会用到图片资源和xib,storyboatd资源,我们可以将这些资源全部归类到bundle文件中,便于管理。
代码传送门
1.创建Bundle资源包
-
1.1新建一个Project
选择
BUndle
,选择第一步创建的WorkSpace
(和SDK开发一中的图1的选择一样),这样就创建好了Bundle
资源工程成功之后的示例
-
1.2 添加资源:图片和xib
-
1.3 修改配置
点击
Build Setting
修改
Base SDK
为iOS
否则编译无法通过修改
COMBINE_HIDPI_IMAGES
为No
否则Bundle中的图片就是tff
格式删除安装路径
Installation Directory
的值。作为资源包,不需要安装 -
1.4 编译后去.bundle资源包
2.将产生的资源和SDK的集成Demo进行关联
-
编译运行后,在
Products
文件找到.bundle
包 右击选择Show in Finder
,找到Debug-iphoneos
文件中.bundle
拖到SDK的集成Demo中。 -
使用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];
运行结果如下
最后的最后,把这个工程上传到Git就可以愉快的开发了。。。以上是我个人的理解,如有错误,请各位大牛批评指正,文中bundle的打包方法参考网上资料
参考链接