ios創建bundle的圖片資源文件

1.首先,我們需要把用到的圖片放到一個文件夾下面,然后更改這個文件夾的名字,在原來的文件名字后面添加.bundle,添加完畢之后,會出現一個彈出框,點擊使用.bundel的按鈕,


1840399-2afbb589fd73d6fb.png

點擊之后,會生成一個帶有.bundle的文件

1840399-de3e1760404c3168.png

或者

002glVnxty6GZINke4G8a&690.jpeg

2.圖片獲得bundle中的資源

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];

或者

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];

3.VC獲得bundle中的資源

NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle" ofType :@ "bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name" bundle:resourceBundle];

當然,可以寫成預編譯語句:

#define MYBUNDLE_NAME @ "MyBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 如果你想將你開發的控件與別人分享,一種方法是直接提供源代碼文件。然而,這種方法并不是很優雅。它會暴露所有的實現細節...
    小熱狗閱讀 3,498評論 3 9
  • (原文:How to Create a Framework for iOS作者:Sam Davies 譯者:Mr_...
    Yaanco閱讀 2,389評論 4 5
  • 原文鏈接在上一篇教程(中譯版)中,你學到了怎么樣創建一個可復用的圓形旋鈕控件。然而你可能不清楚怎樣讓其他開發者更方...
    _冷憶閱讀 2,471評論 9 5
  • 20161203
    宸絲喆時尚女裝閱讀 186評論 1 0
  • 這個名字取的還挺文藝!!!有點兒像人在囧途!今天終于體會到農民工返鄉的感覺了! 今天趕了一天的車,從煙臺坐...
    英倫小兔子閱讀 221評論 1 1