什么是bundle?

bundle就是一个文件夹,按照一定标准组织的目录结构。每个iOS APP至少有一个main bundle,这个main bundle包含了app的二进制代码及任何你用到的资源,如图片,声音,HTML文件等。换句话说,主bundle包含了所有资源,这些资源会被编译成二进制代码提交到App Store上。

bundle与普通的文件夹有什么区别?

1.cocoa touch框架提供了一个接口,可以很方便的访问bundle及其内部资源。

2.如果将bundle加入了Xcode中,则在本地目录下任意更改bundle中的内容,Xcode中的bundle都会察觉到,并且将变化内容同步进来。如果将普通文件夹加入Xcode,在本地目录下删除该目录下的资源,被删除的资源在Xcode中会变成红色,需要手动再处理一遍。总结,bundle中的任何资源变动,Xcode都能同步过去,但普通文件夹却不行。

使用bundle有什么意义?

在我们的APP国际化的时候,bundle就上场了。如果不使用bundle,需要程序员自己维护不同国家和地区使用的资源,有些是公用的,而有些是本地化的,维护成本过高。有了bundle,我们可以按照bundle的标准去存放资源文件,无需写代码判断本地语言。方法很简单,创建对应的“本地化文件夹”,比如需求是不同区域的“test.png”显示的内容不同,我们可以创建两个本地化文件夹zh.lproj和en.lproj,分别把两幅同名但内容不同的test.png放入对应的文件夹中即可。

如何使用bundle?

读取bundle中的image对象:

NSString*alanSugarFilePath = [[NSBundle mainBundle]pathForResource:@"AlanSugar" ofType:@"png"];

if([alanSugarFilePath length]>0){

UIImage*image=[UIImage imageWithContentsOfFile:alanSugarFilePath];

if(image!=nil){

NSLog(@"Successfully loaded the file as an image.");

}else{

NSLog(@"Failed to load the file as an image.");

}

}else{

NSLog(@"Could not find this file in the main bundle.");

}

读取bundle中的NSData对象:

if([alanSugarFilePath length]>0){

NSError   *readError=nil;

NSData  *dataForFile= [[NSData alloc] initWithContentsOfFile:alanSugarFilePath

options:NSMappedRead

error:&readError];

if(readError==nil&&dataForFile!=nil){

NSLog(@"Successfully loaded the data.");

}else if(readError==nil&& dataForFile==nil){

NSLog(@"No data could be loaded.");

}else{

NSLog(@"An error occured while loading data. Error = %@",readError);

}

} else{
            NSLog(@"Could not find this file in the main bundle.");

}

读取子bundle中的NSData对象,如Resources(mainBundle)->Images(childBundle)->AlanSugar.png(file)

NSString*resourcesBundlePath  = [[NSBundle mainBundle]pathForResource:@"Resources" ofType:@"bundle"];

if([resourcesBundlePath length]>0){

NSBundle*resourcesBundle=[NSBundle bundleWithPath: resourcesBundlePath];

if(resourcesBundle!=nil){

NSString*pathToAlanSugarImage=[resourcesBundle pathForResource: @"AlanSugar"

ofType:@"png"

inDirectory:@"Images"];

if([pathToAlanSugarImage length]>0){

UIImage*image=[UIImage  imageWithContentsOfFile: pathToAlanSugarImage];

if(image!=nil){
                                      NSLog(@"Successfully loaded the image from the bundle.");

}else{

NSLog(@"Failed to load the image.");

}

}else{
                          NSLog(@"Failed to find the file inside the bundle.");

}

}else{

NSLog(@"Failed to load the bundle.");

}

} else{
          NSLog(@"Could not find the bundle.");

}

可用[pathsForResourcesOfType:inDirectory: ]方法找到指定文件夹下的所有资源:

NSString*resourcesBundlePath= [[NSBundle mainBundle]pathForResource:@"Resources" ofType:@"bundle"];

if([resourcesBundlePath length]>0){

NSBundle*resourcesBundle=[NSBundle bundleWithPath: resourcesBundlePath];

if(resourcesBundle!=nil){

NSArray*PNGPaths=[resourcesBundle pathsForResourcesOfType:@"png"inDirectory:@"images"];

[PNGPaths enumerateObjectsUsingBlock:^(idobj,NSUInteger idx,BOOL*stop) {

NSLog(@"Path %lu = %@", (unsigned long)idx+1,obj);}];

}else{
                 NSLog(@"Failed to load the bundle.");

}

} else{

NSLog(@"Could not find the bundle.");

}

iOS中bundle的意义的更多相关文章

  1. iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用

    iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用 前言 开发中经常使用三方库去实现某特定功能,而这些三方库通常又分为开源库和闭源库.开源库可以直接拿到源码,和自 ...

  2. iOS中数据库应用基础

    iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...

  3. iOS:iOS中的多控制器管理

    iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@" ...

  4. (转)如何处理iOS中照片的方向

    如何处理iOS中照片的方向 31 May 2015 • 7 min. read • Comments 使用过iPhone或者iPad的朋友在拍照时不知是否遇到过这样的问题,将设备中的照片导出到Wind ...

  5. IOS中无缓存的图片载入

    在IOS中,我们常用[UIImage imageNamed]方法获取图像,这种方法简便,容易理解.但是有个缺点,就是有缓存.这种方式 传人的图像的就是通过文件名方式文件名.如果,我们内存有限,我们就必 ...

  6. ios中的RunLoop 和 android 中的Looper

    今天写android程序,用到了Handler,晚上回来查阅资料,发现了Looper这个概念. 看了一下网上关于Looper的资料,发现这个Looper跟ios中的Runloop基本的理念完全一致! ...

  7. 转iOS中delegate、protocol的关系

    iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...

  8. ios中的category与extension

    http://blog.csdn.net/haishu_zheng/article/details/12873151   category和extension用来做类扩展的,可以对现有类扩展功能或者修 ...

  9. iOS中的NSTimer 和 Android 中的Timer

    首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...

随机推荐

  1. 洛谷P3378堆

    传送门啦 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...

  2. 奇妙的CSS之CSS3新特性总结

    随着CSS3标准的发布,越来越多的浏览器开始支持最新的CSS标准,虽然还有些新特性支持的不够完美,但相信未来的浏览器一定会完全支持CSS3的,毕竟这代表着大趋势!下面l列出来一些CSS3中出现的新特性 ...

  3. Java - 利用StringEscapeUtils对字符串进行各种转义与反转义

    来自:http://blog.csdn.net/chenleixing/article/details/43456987 --------------------------------------- ...

  4. Python学习笔记:startswith & endswith 判断开头结尾是否为指定字符串

    作用: 判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型. 如果以指定后缀结尾返回True,否则返回False. 可选参数"start"与"end"为 ...

  5. JavaScript中构造函数

    构造函数:函数的另一种执行方法,执行后创建对象,并创建原型对象. 原型链:对象访问构造函数的指针. Function函数:函数对象. Object函数:所有创建对象的祖辈对象,也是由Function对 ...

  6. CF2B The least round way 题解

    都是泪呀...↑ 题目传送门 题意(直接复制了QWQ) 题目描述 给定由非负整数组成的\(n \times n\)的正方形矩阵,你需要寻找一条路径: 以左上角为起点, 每次只能向右或向下走, 以右下角 ...

  7. .gitignore文件如何编写?

    .gitignore文件即 项目中不需要被追踪(track)且上传到git系统的文件 <1>忽略文件的原则 a.忽略操作系统自动生成的文件,比如缩略图等 b.忽略编译生成的中间文件.可执行 ...

  8. hdu 5131 (2014广州现场赛 E题)

    题意:对给出的好汉按杀敌数从大到小排序,若相等,按字典序排.M个询问,询问名字输出对应的主排名和次排名.(排序之后)主排名是在该名字前比他杀敌数多的人的个数加1,次排名是该名字前和他杀敌数相等的人的个 ...

  9. linux 101 hacks 2date,grep,find

    感觉挨个按着作者来的太蠢了,我还是放自己觉得不错的东西把 用特定格式显示当前时间 以下的方法可以用各种不同的格式来显示当前时间: $ date Thu Jan :: PST $ date --date ...

  10. 【LOJ】#2028. 「SHOI2016」随机序列

    题解 我们发现只有从第一个往后数,用乘号联通的块是有贡献的 为什么,因为后面所有表达式 肯定会有 + ,还会有个-,贡献全都被抵消了 所以我们处理出前缀乘积,然后乘上表达式的方案数 答案就是\(\su ...