IOS NSBundle的使用,注意mainBundle和Custom Bundle的区别
2016-05-12 11:03
676 查看
1、[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/27/f9b93eb7e587ce6940c9370ba96d00eb.png)
Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样:
2、使用文件夹的时候,Build结果
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/27/9f654f9ef312a062786ba944c2564bd8.png)
依然使用同样的方法,不需要制定文件夹路径
编译之后,mainBundle的资源都是放到RootFolder下,所以,可以直接访问,不要指定内部路径
3、使用其他的Bundle
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/27/fcfecaff9d77a2bdc498420273a898c4.png)
使用Custom bundle,访问内部的子文件夹,需要指定inDirectory参数,这个mainBundle不同,这是为何?
原文地址:http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/27/f9b93eb7e587ce6940c9370ba96d00eb.png)
Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/27/9f654f9ef312a062786ba944c2564bd8.png)
依然使用同样的方法,不需要制定文件夹路径
//inDirectory参数可有可无
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png" inDirectory:@"Images/MoreImages"]; NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png"];
编译之后,mainBundle的资源都是放到RootFolder下,所以,可以直接访问,不要指定内部路径
3、使用其他的Bundle
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/27/fcfecaff9d77a2bdc498420273a898c4.png)
NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"Resources" ofType:@"bundle"]; NSLog(@"resourceBundle: %@", resourceBundle); NSString *earth = [[NSBundlebundleWithPath:resourceBundle] pathForResource:@"Matthijs"ofType:@"jpg"inDirectory:@"Images/MoreImages"]; NSLog(@"path: %@", earth);
使用Custom bundle,访问内部的子文件夹,需要指定inDirectory参数,这个mainBundle不同,这是为何?
注意:Since bundles other than the main bundle can have folders embedded inside them, to access files inside folders of a bundle other than the main bundle it is best to use the pathFor Resource:ofType:inDirectory: method of NSBundle to explicitly specify the folder in which a specific file/resource exists.
原文地址:http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html
相关文章推荐
- iOS有三种多线程编程的技术
- IOS NSBundle 的理解和 mainBundle 类方法详解
- iOS AFNetWorking下同步异步请求
- genymotion VT-x is disabled in the bios for both all CPU modes
- iOS keyboard的types return变成什么样
- iOS 更改navigationcontroller pop出来的视图返回按钮
- iOS CoreLocation框架第二章—— CLLocationManagerDelegate全解
- iOS tabbar视图中隐藏tabbar
- iOS指定程序启动后初始的ViewController
- iOS实现图片的缩放和居中显示
- iOS中使用nil NULL NSNULL的区别
- iOS 引入支付宝 缺少 #include <openssl/asn1.h>
- iOS 集合的深复制与浅复制
- iOS服务器json数据中null的处理
- iOS 本地数据 存取 详解
- iOS开发之多线程—GCD介绍
- 带着大家走进iOS单元测试世界
- ios nil Nil Null NSNull的区别
- ios nil Nil Null NSNull的区别
- ios nil Nil Null NSNull的区别