1、Bundle 文件
Bundle 文件,就是資源文件包。我們將許多圖片、XIB、文本文件組織在一起,打包成一個 Bundle 文件。方便在其他項目中引用包內的資源。
Bundle 文件是靜態的,也就是說,我們包含到包中的資源文件作為一個資源包是不參加項目編譯的。也就意味着,bundle 包中不能包含可執行的文件。它僅僅是作為資源,被解析成為特定的二進制數據。
2、制作 Bundle 文件
1、新建 Bundle 項目
創建名為 yooweiSourcesBundle(最后要生成的 Bundle 文件名稱)的工程,注意 Bundle 默認是 macOS 系統的,Xcode 高版本中需要在macOS => Framework & Library 選項下找到。具體創建路徑如下:
打開Xcode, 選擇File ----> New ---> Project,選擇macOS ----> Framework & Library ---> Bundle。


按照引導一步一步來,最后選擇存放的位置,那么一個一個bundle就創建好了。
補充注意:如果已經在靜態庫工程中,想新增一個資源bundle的話,需要按照如下圖所示的方式進行

2、修改 Bundle 配置信息
(1)因為 Bundle 默認是 macOS 系統的,所以需要修改他的信息,修改成 iOS 系統。


(2)設置 Build Setting 中的 COMBINE_HIDPI_IMAGES 為 NO,否則 Bundle 中的圖片就是 tiff 格式了。

3、可選配置
作為資源包,僅僅需要編譯就好,無需安裝相關的配置,設置 Skip Install 為 NO。同樣要刪除安裝路徑 Installation Directory 的值。


該資源包的 pch 文件和 strings 文件是可以刪除(老版本Xcode會有,高版本沒有)。
4、添加文件
將資源文件或文件夾拖動到工程中的 SourcesBundle 文件夾下面。

5、編譯生成 Bundle 文件
我們分別選擇 Generic iOS Device 和任意一個模擬器各編譯一次,編譯完后,我們會看到工程中 Products 文件夾下的 SourcesBundle.bundle 由紅色變成了黑色。

然后 show in finder,看看生成的文件。我們看到它為真機和模擬器都生成了 .bundle 資源文件。

選中 .bundle 文件右鍵 顯示包內容,我們可以看到之前拖拽到工程中的資源文件都在其中。
3、使用 Bundle 文件
打開Xcode, 選擇File ----> New ---> Project,選擇iOS ----> Application ---> single View App 創建一個yooweiLiveDemo 用來測試bundle里面文件的使用。將生成的真機(Debug-iphoneos)Bundle 資源文件拖拽到需要使用的工程中。

1、加載 Bundle 中的 xib 資源文件

-(void)useXIB{
// 設置文件路徑
// NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"yooweiSourcesBundle" ofType:@"bundle"];
// NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
NSBundle *resourceBundle=[NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"yooweiSourcesBundle" withExtension:@"bundle"]];
// 加載 nib 文件
UINib *nib = [UINib nibWithNibName:@"WYLiveCell" bundle:resourceBundle];
NSArray *viewObjs = [nib instantiateWithOwner:nil options: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];
}
2、加載 Bundle 中的圖片資源文件

-(void)useImage{
// 1 指定絕對路徑的形式
UIImage *imageOne = [UIImage imageNamed:@"yooweiSourcesBundle.bundle/loading_yoowei.png"];
UIImageView *one=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
one.image=imageOne;
[self.view addSubview:one];
// 拼接路徑的形式
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"yooweiSourcesBundle" ofType:@"bundle"];
NSString *imgPathTwo= [bundlePath stringByAppendingPathComponent:@"loading_yoowei"];
UIImage *imageTwo = [UIImage imageWithContentsOfFile:imgPathTwo];
UIImageView *two=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 30, 30)];
two.image=imageTwo;
[self.view addSubview:two];
// 宏定義的形式
#define MYBUNDLE_NAME @"yooweiSourcesBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath:MYBUNDLE_PATH]
NSString *imgPathThree= [MYBUNDLE_PATH stringByAppendingPathComponent:@"loading_yoowei"];
UIImage *imageThree = [UIImage imageWithContentsOfFile:imgPathThree];
UIImageView *three=[[UIImageView alloc]initWithFrame:CGRectMake(90, 90, 30, 30)];
three.image=imageThree;
[self.view addSubview:three];
}
3、加載其他資源文件

-(void)uesOther{
// 再比如獲取靜態庫里面bundle里面的資源文件證書
NSString *pathStr = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yooweiSourcesBundle.bundle"];
NSBundle *pathBundle = [NSBundle bundleWithPath:pathStr];
//獲取Bundle里的資源路徑
NSString *cacerPath = [pathBundle pathForResource:@"ca" ofType:@"cer"];
NSData *cerDate=[NSData dataWithContentsOfFile:cacerPath];
NSLog(@"%@",cerDate);
}
效果如下:

