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 ...
随机推荐
- 洛谷P1725 琪露诺
传送门啦 本人第一个单调队列优化 $ dp $,不鼓励鼓励? 琪露诺这个题,$ dp $ 还是挺好想的对不,但是暴力 $ dp $ 的话会 $ TLE $ ,所以我们考虑用单调队列优化. 原题中说她只 ...
- MySQL学习笔记:delete
使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录. 语法: DELETE FROM table_name [WHERE Clause] 如果没有指定 WHERE 子句, ...
- 关于ARM指令那些你必须知道的东西
1.32位ARM指令每一位都有其作用,具体如下: 低12为第二操作数, 12~15位为目的寄存器, 16~19位为第一操作数, 20~27就是操作码, 28~31就是条件域. 2.多寄存器load和s ...
- CCF CSP 201709-3 JSON查询
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201709-3 JSON查询 问题描述 JSON (JavaScript Object Not ...
- HBase(六)HBase整合Hive,数据的备份与MR操作HBase
一.数据的备份与恢复 1. 备份 停止 HBase 服务后,使用 distcp 命令运行 MapReduce 任务进行备份,将数据备份到另一个地方,可以是同一个集群,也可以是专用的备份集群. 即,把数 ...
- 8-15 Shuffle uva12174
题意: 你正在使用的音乐播放器有一个所谓的乱序功能,即随机打乱歌曲的播放顺序.假设一共有s首歌,则一开始会给这s首歌随机排序,全部播放完毕后再重新随机排序.继续播放,依此类推.注意,当s首歌播放完毕之 ...
- mysql插入数据时,去掉重复的数据;
1. 利用insert ignore into语句去重 mysql> INSERT IGNORE INTO person_tbl (last_name, first_name) -> VA ...
- react篇章-React State(状态)-组件都是真正隔离的
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- idea导入或打开项目配置问题
learn项目遇到问题: 1.IntelliJ Idea编译报错:请使用 -source 7 或更高版本以启用 diamond 运算符 file - project structure或者直接快捷键: ...
- 提高django model效率的几个小方法
django的model效率不是很高,特别是在做大量的数据库操作的时候,如果你只用django来开企业站或者外包项目的话,那可以小跳过下,而你恰巧是效率狂或者说是对程序的效率要求比较高的话,那就要注意 ...