hi,勇敢的小夥伴兒們大家好,昨天一位朋友問我如何處理項目裏的圖片,是放在Assets.xcassets嗎,我說是啊,
他問~
“你會打包成Bundle嗎?”
“啊?我不會。”
“不會學啊,愣着幹嘛?”
“噢,抽時間吧。”
於是這篇文章在這個午休期間應運而生。
以下正文:
什麼是Bundle文件?
簡單理解,就是資源文件包。我們將許多圖片、XIB、文本文件組織在一起,打包成一個Bundle文件。方便在其他項目中引用包內的資源。
Bundle文件的特點?
1.新建一個工程macOS的Bundle項目
2.命名
3.刪除文件夾和info.plist文件
4.刪除Build Settings裏的Packaging的info.plist的文件地址
5.設置Base SDK爲iOS
6.添加要打包的資源文件進到文件夾
7.我添加了一張圖片
8.編譯,然後找到下圖的按鈕打開文件夾
9.找到Bundle文件,右鍵顯示包內容,查看是否將資源文件加入其中
10.檢查無誤之後,將Bundle拷貝到iOS項目中
11.開始使用
這是一種方法,還有其他方法。
NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle"ofType :@ "bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name"bundle:resourceBundle];
或者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];
#define MYBUNDLE_NAME @ "MyBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]