資源文件打成靜態(tài)包 ——.bundle文件

480605.jpg

什么是Bundle文件?

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


0AABB11A-CE09-4E77-ADA2-B059894483B0.png

Bundle文件的特點?

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

EE70F51F-B448-4153-A2DA-BD07091CBA2B.png

項目集成bundle

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

74322A7A-5BC0-412C-808F-69AB7F3D1BD3.png

使用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>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容