NSBundle--获取文件路径
来源:互联网 发布:mac显示硬盘 编辑:程序博客网 时间:2024/05/22 04:38
之前在初始化一个类的时候:TestViewController *viewcontroller=[[TestViewController alloc]initWithNibName:@"TestViewController" bundle:[NSBundle mainBundle]];不是很明白:[NSBundle mainBundle]的意思。后来查阅资料后知道了它的作用,如下:
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle。
通过使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];
一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
一旦我们有了NSBundle 对象,那么就可以访问其中的资源了
NSBundle束,是一种特定的文件类型,其中的内容遵循特定的结构。
NSBundle的一个主要作用是 获取Resources文件夹中的资源。
在编程中使用[NSData dataWithContentOfFile:@"foo"]的时候,总是无法读取正确的文件内容。而使用[NSData dataWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”foo” ofType:@”"]的时候就可以。
一.获取图片
1. NSString *path = [[NSBuddle mainBuddle] pathForResource:@"resourceName" oftype@"resourceType"];
UIImage *image = [[UIImage imageWithContentsOfFile:path];
2. UIImage *image = [UIImage imageNamed:@"imageName"];
二.获取plist文件
NSArray *array =[[NSArrayalloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"name"ofType:@"plist"]];
NSDictionary *dict=[arrayobjectAtIndex:index];//将plist文件中的内容转换成字典
- NSBundle--获取文件路径
- NSBundle--获取文件路径
- NSBundle--获取文件路径
- NSBundle--获取文件路径
- NSBundle--获取文件路径
- NSBundle--获取文件路径
- 关于NSBundle获取文件路径的问题
- NSBundle 获取文件路径为空
- NSBundle 获取资源路径
- NSBundle 获取不到路径
- 通过NSBundle获取文件路径的细节问题
- 利用NSBundle获取文件
- NSBundle方法获取不到资源路径
- NSBundle介绍以及读取沙盒文件路径问题
- ios NSBundle 程序包路径
- NSBundle路径问题
- NSBundle MainBundle的路径
- NSBundle(获取资源路径方法)的相关使用方法和注意事项
- js数组的操作
- 学驾照考交规去上海公安培训总部考试全过程
- php解压缩类并带实例[linux window平台皆通过
- cocosBuiler使用简介&心得
- apache和tomcat相结合使用实现伪静态
- NSBundle--获取文件路径
- ant安装运行及项目的构建<一>
- 外贸B2C收款结汇的秘密
- 【C++面试题】:用两个队列模拟一个栈
- IplImage结构
- 回溯算法---八皇后问题
- oracle中rownum的用法
- 二叉树的应用—最优二叉树(哈夫曼树)
- Oracle的字符串和日期的比较