簡(jiǎn)單的說(shuō)霎褐,就是資源文件包址愿。我們將許多圖片、XIB冻璃、文本文件組織在一起响谓,打包成一個(gè)Bundle文件。方便在其他項(xiàng)目中引用包內(nèi)的資源省艳。
Bundle 文件是靜態(tài)的娘纷,也就是說(shuō),我們包含到包中的資源文件作為一個(gè)資源包是不參加項(xiàng)目編譯的跋炕。bundle 包中不能包含可執(zhí)行的文件赖晶。它僅僅是作為資源,被解析成為特定的二進(jìn)制數(shù)據(jù)辐烂。
1. 創(chuàng)建 Bundle
遏插。
新建macOS 下的Framework & Library 捂贿,點(diǎn)擊bundle,然后下一步給bundle取個(gè)名字
2. 修改設(shè)置(因?yàn)锽undle默認(rèn)是macOS的胳嘲,所以需要修改他的信息眷蜓,修改成iOS的)
Base SDK選擇iOS
修改為NO,否則圖片格式就是tiff格式
- 可選配置
設(shè)置Skip Install為YES胎围, Installation Directiotory刪除后邊的路徑
3. 在 Bundle
工程中創(chuàng)建 Assets
文件
這個(gè)Assets無(wú)論是macOS或者是iOS下都有
4. 把準(zhǔn)備好的圖片拖入 Assets
下吁系。
image.png
5.然后編譯篮幢,選擇 Generic iOS Device
或者任意一個(gè)模擬器各編譯一次笛质,編譯完后,我們會(huì)看到工程中 Products
文件夾下的 .bundle
由紅色變成了黑色何缓。
Show in Finder找到文件
6. 關(guān)于bundle資源包的使用
- 將要使用的資源包拖入要使用的項(xiàng)目中福荸。
代碼中出現(xiàn)的CeShiBundle和MyBundle都是打包好的資源包的名稱(chēng)
// 設(shè)置文件路徑
NSString *bundlePath = [[NSBundlemainBundle]pathForResource:@"CeShiBundle"ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundlebundleWithPath:bundlePath];
// 加載 nib文件
UINib *nib = [UINibnibWithNibName:@"Demo"bundle:resourceBundle];
NSArray *viewObjs = [nibinstantiateWithOwner:niloptions:nil];
// 獲取 xib文件
UIView *view = viewObjs.lastObject;
view.frame = CGRectMake(20,50,self.view.bounds.size.width -40,self.view.bounds.size.width -40);
[self.view addSubview:view];
// 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 = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];
/**********************加載 Bundle中的圖片資源文件***********************/
// 指定絕對(duì)路徑
UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/demo.jpg"];
// 拼接路徑
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MyBundle" ofType:@"bundle"];
NSString *imgPath= [bundlePath stringByAppendingPathComponent:@"sb"];
UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
// 宏定義
#define MYBUNDLE_NAME @"MyBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath:MYBUNDLE_PATH]
NSString *imgPath= [MYBUNDLE_PATH stringByAppendingPathComponent:@"demo4"];
UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
/**********************bundle的使用三***********************/
// 在控制器中加入一下代碼
-(instancetype)init{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MyBundle"ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
self=[super initWithNibName:@"MyBundlevc"bundle:resourceBundle];
return self;
}
/**********************加載bundle中的xib生成的cell***********************/
加載nib的時(shí)候使用以下代碼蕴坪,最主要的是表明是從那個(gè)bundle中獲取nib;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MyBundle"ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
UINib *nib=[UINib nibWithNibName:@"MyBundlecell" bundle:resourceBundle];
[tab registerNib:nib forCellReuseIdentifier:identifier];