bundle 文件的理解
mian bundle
文件說(shuō)明
參考:IOS開(kāi)發(fā)NSBundle對(duì)象使用詳解 https://my.oschina.net/u/874588/blog/98342
應(yīng)用程序就是是一個(gè) bundle
在Finder中,一個(gè)應(yīng)用程序看上去和其他文件沒(méi)有什么區(qū)別,但是實(shí)際上它是一個(gè)包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個(gè)目錄叫做程序的 main bundle
。也就是說(shuō),我們獲取的 NSBundle mainBundle
就是我們的 應(yīng)用程序
,或者說(shuō)應(yīng)用程序的 根目錄(rootFolder)
注意
在工程中我們可能會(huì)自己建立一個(gè)
main.bundle
文件,但不是上面說(shuō)到的資源目錄main bundle
文件參考:
NSBundle的使用,注意mainBundle和Custom Bundle的區(qū)別 http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html
1,獲取應(yīng)用程序 main bundle
文件
NSBundle *myBundle = [NSBundle mainBundle];
2,獲取資源文件下的 .bundle
文件
如:source.bundle
如果想獲取到資源目錄(main bundle
)下的 bundle
文件,比如 source.bundle
文件,首先要進(jìn)入到應(yīng)用程序 main bundle
目錄下
// 獲取 main bundle 目錄
NSBundle *mainBundle = [NSBundle mainBundle];
// 獲取 main bundle 路徑
NSString *mainPath = [mainBundle resourcePath];
NSLog(@"mainPath 路徑: %@", mainPath);
mainPath 路徑:
/Users/yang_0921/Library/Developer/CoreSimulator/Devices/7FFAA7A9-7B04-4681-B9CB-838EA92A2829/data/Containers/Bundle/Application/30737520-1371-4CCF-9E9F-805BC414A5F6/bundle.app
在拼接 main.bundle
路徑
NSString *sourcePath = [mainPath stringByAppendingPathComponent:@"source.bundle"];
sourcePath:
/Users/yang_0921/Library/Developer/CoreSimulator/Devices/7FFAA7A9-7B04-4681-B9CB-838EA92A2829/data/Containers/Bundle/Application/55081882-2121-4FBC-8E9E-C38332D58489/bundle.app/source.bundle
獲取 source.bundle
文件
NSBundle *sourceBundle = [NSBundle bundleWithPath:sourcePath];
sourceBundle:
</Users/yang_0921/Library/Developer/CoreSimulator/Devices/7FFAA7A9-7B04-4681-B9CB-838EA92A2829/data/Containers/Bundle/Application/55081882-2121-4FBC-8E9E-C38332D58489/bundle.app/source.bundle> (not yet loaded)
到這里,我們就成功的獲取到了 source.bundle
文件。
接著,獲取 source.bundle
中的圖片
NSString *imagePath = [sourceBundle pathForResource:@"sample" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
self.imageView.image = image;
3,獲取 .bundle
子目錄下的圖片
如果要獲取上面截圖中的 source.bundle/sub
文件夾下的 sample@3x.png
圖片,需要進(jìn)入到 sub
文件夾下訪問(wèn),通過(guò) sub/xxx
的方式
NSString *imagePath = [sourceBundle pathForResource:@"sub/sample@3x" ofType:@"png"];
// NSString *imagePath = [sourceBundle pathForResource:@"sample@3x" ofType:@"png" inDirectory:@"sub"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
本地資源目錄下獲取文件時(shí),可以不使用
/sub
或inDirectory:@"sub"
去指定詳細(xì)的文件位置,這是因?yàn)椋壕幾g之后,mainBundle的資源都是放到RootFolder下,所以,可以直接訪問(wèn),不要指定內(nèi)部路徑。但是自定義的bundle
文件是不同的。具體參考:
NSBundle的使用,注意mainBundle和Custom Bundle的區(qū)別 http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html
bundle
的其他用途
1,獲取 nib
文件
通過(guò) bundle
獲取 nib
文件
SourceView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([SourceView class]) owner:nil options:nil] firstObject];
NSLog(@"view = %@", view);
view = <SourceView: 0x7fbabee0d150; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x600000034f00
一個(gè)思考
在我們使用
nib
文件自定義table view cell
的時(shí)候,通常通過(guò)下面方法注冊(cè)cell
,其中使用[UINib nibWithNibName:NSStringFromClass(className) bundle:nil]
獲取nib
文件[tableView registerNib:[UINib nibWithNibName:NSStringFromClass(className) bundle:nil] forCellReuseIdentifier:@"cellId"];
這個(gè)方法中最后一個(gè)參數(shù)
bundle:
是否可以是在我們自動(dòng)的source.bundle
下的文件?
查看文檔說(shuō)明:
Paste_Image.png
bundle
是我們要獲取nib
的目錄,nil
為[NSBundle mainBundle]
,說(shuō)明可以獲取bundle
中的nib
文件。嘗試
但是頭文件并不能夠訪問(wèn)的到
使用 @class TableViewCell;
運(yùn)行
報(bào)錯(cuò):不能夠加載 source.bundle
下的 TableViewCell
所以,暫時(shí)還不知道如何實(shí)現(xiàn)???
2,bundle中可以包含一個(gè)庫(kù). 如果我們從庫(kù)得到一個(gè)class, bundle會(huì)連接庫(kù),并查找該類(lèi)
Class newClass = [[NSBundle mainBundle] classNamed:@"test"];
id newInstance = [[newClass alloc] init];
如果不知到class名,也可以通過(guò)查找主要類(lèi)來(lái)取得(我沒(méi)有實(shí)驗(yàn)成功)
Class aClass = [[NSBundle mainBundle] principalClass];
id anInstance = [[aClass alloc] init];
3,獲取本地目錄下的文件內(nèi)容,獲取到路徑之后,通過(guò)對(duì)于的方法獲取
一般格式:
// 獲取XML文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"re" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
// [NSData dataWithContentsOfFile:<#(nonnull NSString *)#>]
// 獲取字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]];
// 獲取數(shù)組
[NSArray arrayWithContentsOfFile:<#(nonnull NSString *)#>]