標籤:
在我們使用第三方架構時,常常看到XXX.bundle的檔案。我們找到該檔案,顯示包內容,大致看到很多資源檔:圖片、配置文本、XIB檔案……
什麼是Bundle檔案?簡單理解,就是資源檔包。我們將許多圖片、XIB、文字檔組織在一起,打包成一個Bundle檔案。方便在其他項目中引用包內的資源。
Bundle檔案的特點?Bundle是靜態,也就是說,我們包含到包中的資源檔作為一個資源套件是不參加項目編譯的。也就意味著,bundle包中不能包含可執行檔檔案。它僅僅是作為資源,被解析成為特定的2進位資料。
製作Bundle
1.建立bundle項目
2.添加需要的圖片加入你需要編譯在bundle中的資源檔。當然,預設的配置也是可以的,如果你需要特定的最佳化或者特定的路徑配置,你可以進行下面第3步的配置。
3.你可以對編譯的bundle進行一些可選的設定(可選)
a.作為資源套件,僅僅需要編譯就好,無需安裝相關的配置。
b.同樣要刪除安裝路徑。
c.該資源套件的pch檔案和strings檔案是可以刪除的。
4.最好狀態下,要編譯出適用與iPhone的bundle檔案。
項目整合bundle 使用bundle就非常的easy了,將編譯好的XXXX.bundle 檔案直接加入到需要的項目中。省略了!
使用bundle中的資源將要使用的bundle整合到項目中後,就可以使用了。需要注意的就是,bundle是靜態,不進行編譯的資源檔。所以,要使用bundle中的資源,就需要找到相應的資源路徑。這裡廢話就不多說了,貼代碼!
VC獲得bundle中的資源
NSString * bundlePath = [[NSBundle mainBundle] pathForResource: @ "MyBundle" ofType :@ "bundle"];
NSBundle *resourceBundle =[NSBundle bundleWithPath:bundlePath];
UIViewController *vc =[[UIViewController alloc] initWithNibName:@"vc_name" bundle:resourceBundle];
圖片獲得bundle中的資源
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50,50, 50,50)];
UIImage *image = [UIImageimageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];
或者
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50,50, 50,50)];
NSString *imgPath= [bundlePathstringByAppendingPathComponent:@"img_collect_success.png"];
UIImage *image_1=[UIImage imageWithContentsOfFile:imgPath];
[imgView setImage:image_1];
當然,可以寫成先行編譯語句:
#define MYBUNDLE_NAME @ "MyBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
飛機票:http://blog.sina.com.cn/s/blog_7b9d64af0101jmj2.html
希望對你有所協助!
iOS-產生Bundle包-引入bundle-使用bundle