iOS中bundle的意义
什么是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的意义的更多相关文章
- iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用
iOS 中 .a 和 .framework 静态库的创建与 .bundle 资源包的使用 前言 开发中经常使用三方库去实现某特定功能,而这些三方库通常又分为开源库和闭源库.开源库可以直接拿到源码,和自 ...
- iOS中数据库应用基础
iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...
- iOS:iOS中的多控制器管理
iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@" ...
- (转)如何处理iOS中照片的方向
如何处理iOS中照片的方向 31 May 2015 • 7 min. read • Comments 使用过iPhone或者iPad的朋友在拍照时不知是否遇到过这样的问题,将设备中的照片导出到Wind ...
- IOS中无缓存的图片载入
在IOS中,我们常用[UIImage imageNamed]方法获取图像,这种方法简便,容易理解.但是有个缺点,就是有缓存.这种方式 传人的图像的就是通过文件名方式文件名.如果,我们内存有限,我们就必 ...
- ios中的RunLoop 和 android 中的Looper
今天写android程序,用到了Handler,晚上回来查阅资料,发现了Looper这个概念. 看了一下网上关于Looper的资料,发现这个Looper跟ios中的Runloop基本的理念完全一致! ...
- 转iOS中delegate、protocol的关系
iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...
- ios中的category与extension
http://blog.csdn.net/haishu_zheng/article/details/12873151 category和extension用来做类扩展的,可以对现有类扩展功能或者修 ...
- iOS中的NSTimer 和 Android 中的Timer
首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...
随机推荐
- java 学习网站
http://how2j.cn/ 教学网站 慕课视频下载网站 http://www.feemic.cn/mooc //慕课搜索和下载的网站http://www.soshoulu.com/tools/ ...
- JavaWeb--中文乱码小结
JavaWeb--中文乱码小结 出处:http://chriszz.sinaapp.com0.纯粹html乱码: 换个editor吧(有时候notepad都比sublime_text好用),最好是在& ...
- 提高eclipse使用效率(二)—— 提高Android开发效率的小技巧
XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - X ...
- markdown转换为html
想要自己实现markdown编辑器,欲使用markdown-it作为编辑器,有着比较多的插件,可以实现代码高亮以及对数学公式转换等功能. // Activate/deactivate rules, w ...
- day9--paramiko模块
志不坚者智不达 paramiko:在Linux链接其他机器,每台Linux机器都有一个SSHClient:Python自己也写了一个SSHClient,那么Python写paramiko创建SSHCl ...
- nullptr
以前都是用0来表示空指针的,但由于0可以被隐式类型转换为整形,这就会存在一些问题.关键字nullptr是std::nullptr_t类型的值,用来指代空指针.nullptr和任何指针类型以及类成员指针 ...
- Ionic Js十一:模态
ionicModal 可以遮住用户主界面的内容框. 你可以在你的 index 文件或者是其他文件内嵌入以下代码(里面的代码可以根据你自己的业务场景相应的改变). <script id=" ...
- thinkphp结合ajax实现统计页面pv的浏览量
统计pv量很常用,下面的代码用ajax实现的,使用ajax可以避免页面缓存造成的影响,只要客户端的js代码执行了就可以统计流量. 一共就两部 将下面代码放在要统计的html页面中,测试时把地址换成自己 ...
- thinkphp数据查询方法总结select ,find,getField,query
thinkphp已经封装好了常用的查询方法,且都比较实用,对于不常用的查询框架也保留了原始查询方法query. 1 2 $Model = new Model() // 实例化一个model对象 没有对 ...
- 使用chrales抓包IOS的https(pc+手机)
1.安装SSL证书到手机 点击 Help -> SSL Proxying -> Install Charles Root Certificate on a Mobile Device 2. ...