bundle 文件的理解

9 篇文章 0 订阅

【知识总结】bundle 文件的理解

96
小子爱搞事
2017.02.17 14:13* 字数 882 阅读 1182 评论 0

bundle 文件的理解

mian bundle 文件说明

参考:IOS开发NSBundle对象使用详解 https://my.oschina.net/u/874588/blog/98342

应用程序就是是一个 bundle

在Finder中,一个应用程序看上去和其他文件没有什么区别,但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的 main bundle。也就是说,我们获取的 NSBundle mainBundle 就是我们的 应用程序,或者说应用程序的 根目录(rootFolder)

Paste_Image.png

注意

在工程中我们可能会自己建立一个 main.bundle 文件,但不是上面说到的资源目录 main bundle 文件

参考:

NSBundle的使用,注意mainBundle和Custom Bundle的区别 http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html

Paste_Image.png
1,获取应用程序 main bundle 文件
NSBundle *myBundle = [NSBundle mainBundle];
2,获取资源文件下的 .bundle 文件

如:source.bundle

Paste_Image.png

如果想获取到资源目录(main bundle)下的 bundle 文件,比如 source.bundle 文件,首先要进入到应用程序 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;
Paste_Image.png
3,获取 .bundle 子目录下的图片

如果要获取上面截图中的 source.bundle/sub 文件夹下的 sample@3x.png 图片,需要进入到 sub 文件夹下访问,通过 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];
Paste_Image.png

本地资源目录下获取文件时,可以不使用 /subinDirectory:@"sub" 去指定详细的文件位置,这是因为:编译之后,mainBundle的资源都是放到RootFolder下,所以,可以直接访问,不要指定内部路径。但是自定义的 bundle 文件是不同的。具体参考:
NSBundle的使用,注意mainBundle和Custom Bundle的区别 http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html


bundle 的其他用途
1,获取 nib 文件

通过 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

一个思考

在我们使用 nib 文件自定义 table view cell 的时候,通常通过下面方法注册 cell,其中使用 [UINib nibWithNibName:NSStringFromClass(className) bundle:nil] 获取 nib 文件

[tableView registerNib:[UINib nibWithNibName:NSStringFromClass(className) bundle:nil] forCellReuseIdentifier:@"cellId"];

这个方法中最后一个参数 bundle:是否可以是在我们自动的 source.bundle 下的文件?
查看文档说明:

Paste_Image.png

bundle 是我们要获取 nib 的目录, nil[NSBundle mainBundle],说明可以获取 bundle 中的 nib 文件。

尝试
Paste_Image.png

但是头文件并不能够访问的到

Paste_Image.png

使用 @class TableViewCell; 运行

Paste_Image.png

报错:不能够加载 source.bundle 下的 TableViewCell
所以,暂时还不知道如何实现???

2,bundle中可以包含一个库. 如果我们从库得到一个class, bundle会连接库,并查找该类
Class newClass = [[NSBundle mainBundle] classNamed:@"test"];
id newInstance = [[newClass alloc] init];

如果不知到class名,也可以通过查找主要类来取得(我没有实验成功)

Class aClass = [[NSBundle mainBundle] principalClass];
id anInstance = [[aClass alloc] init];
3,获取本地目录下的文件内容,获取到路径之后,通过对于的方法获取

一般格式:

Paste_Image.png
// 获取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"]];

// 获取数组
[NSArray arrayWithContentsOfFile:<#(nonnull NSString *)#>]
      </div>
    </div>
</div>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值