
什么是Bundle文件?
簡單理解,就是資源文件包。我們將許多圖片、XIB、文本文件組織在一起,打包成一個Bundle文件。方便在其他項目中引用包內(nèi)的資源。

Bundle文件的特點?
Bundle是靜態(tài)的,也就是說,我們包含到包中的資源文件作為一個資源包是不參加項目編譯的。也就意味著,bundle包中不能包含可執(zhí)行的文件。它僅僅是作為資源,被解析成為特定的2進制數(shù)據(jù)。

項目集成bundle
使用bundle就非常的easy了,將編譯好的XXXX.bundle 文件直接加入到需要的項目中。(拖進去就??)省略了!

使用bundle中的資源
將要使用的bundle集成到項目中后,就可以使用了。需要注意的就是,bundle是靜態(tài)的,不進行編譯的資源文件。所以,要使用bundle中的資源,就需要找到相應的資源路徑。
VC獲得bundle中的資源
<pre>
NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle"ofType :@ "bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name"bundle:resourceBundle];
</pre>
圖片獲得bundle中的資源
<pre>
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
UIImage *image = [UIImageimageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];
</pre>
或者
<pre>
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
NSString *imgPath= [bundlePath stringByAppendingPathComponent:@"img_collect_success.png"];
UIImage *image_1=[UIImage imageWithContentsOfFile:imgPath];
[imgView setImage:image_1];
</pre>
當然,可以寫成預編譯語句:
<pre>
define MYBUNDLE_NAME @ "MyBundle.bundle"
define MYBUNDLE_PATH [[[NSBundlemainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
define MYBUNDLE[NSBundle bundleWithPath: MYBUNDLE_PATH]
</pre>