iOS-產生Bundle包-引入bundle-使用bundle

來源:互聯網
上載者:User

標籤:

在我們使用第三方架構時,常常看到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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.