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]