参考文章
制作APP中,我们可以用bundle来管理资源文件.
一、什么是Bundle文件
简单理解,就是资源文件包。我们将许多图片、XIB、文本文件组织在一起,打包成一个Bundle文件。方便在其他项目中引用包内的资源。
二、Bundle文件的特点
Bundle是静态的,也就是说,我们包含到包中的资源文件作为一个资源包是不参加项目编译的。也就意味着,bundle包中不能包含可执行的文件。它仅仅是作为资源,被解析成为特定的2进制数据。
三、制作Bundle
1.新建bundle项目


2.添加需要的资源文件
加入你需要编译在bundle中的资源文件。

3.编译bundle

4.项目集成bundle
将制作好的bundle拖拽到项目中.
5.使用bundle中的资源

#define MYBUNDLE_NAME_2 @"BundleTest2.bundle"
#define MYBUNDLE_PATH_2 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME_2]
#define MYBUNDLE_2 [NSBundle bundleWithPath: MYBUNDLE_PATH_2]
#define MYBUNDLE_NAME_3 @"Bundle3.bundle"
#define MYBUNDLE_PATH_3 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME_3]
#define MYBUNDLE_3 [NSBundle bundleWithPath: MYBUNDLE_PATH_3]

// 从bundle中加载xib
UIView *view = [[MYBUNDLE_2 loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
view.frame = self.view.frame;
[self.view addSubview:view];
// 从bundle中加载图片
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageWithContentsOfFile:[MYBUNDLE_PATH_2 stringByAppendingPathComponent:@"Contents/Resources/share_pengyouquan"]];
[view addSubview:imageView];
// 从另一个bundle中加载storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Bundle3Storyboard" bundle:MYBUNDLE_3];
NSLog(@"storyboard = %@",storyboard);
// 从另一个bundle中加载图片
UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(250, 100, 100, 100)];
imageView2.image = [UIImage imageWithContentsOfFile:[MYBUNDLE_PATH_3 stringByAppendingPathComponent:@"Contents/Resources/guanyudingyue"]];
[view addSubview:imageView2];


网友评论
Add file to "xxx"...
Options 选择 Create folder references
选择一个文件夹导入
然后修改导入的文件夹名为xxx.bundle,就变成一个bundle了
往bundle中添加文件,拖入文件,就可以使用,不需要编译