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.");

}

时间: 2024-08-02 09:07:19

iOS中bundle的意义的相关文章

iOS中bundle的应用

iOS中的bundle用来保存图片.plist文件.nib文件.国际化文件等资源文件的,根据其应用分为OS X下的bundle工程[1]或target.bundle文件. bundle工程的创建参考文献1和文献2,使用bundle工程注意要先创建Workspace,然后再创建主工程,然后再创建bundle工程.文献1注意的地方1:我们需要修改scheme:Edit Scheme->Build->Add this bundle(点击左下角的+按钮,然后选中要加入的bundle).需要注意的地方2

iOS开发:iOS中的多控制器管理

iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Apply" bundle:nil]; SchemeViewController *schemeVC = [storyboard instantiateViewControllerWithIdentifier:@"SchemeViewController"]; 2.指定xib文

善用iOS中webview(转)

iOS开发中webview和native code写这是一件纠结的事?我写这篇文章, 介绍一下我做iOS两年来总结的一些在webview和native code的配合上的一些经验和技巧,当然,都是基于互联网App的,希望对大家有所帮助. 首先提两句两者的优劣?webview与运维成本低, 更新几乎不依赖App的版本:但在交互和性能上与跟native code有很大差距.native code与之对应. 注,我这里不说HTML5,因为我认为,HTML5确实给web带入了一个新时代.这个时代是什么,

IOS中使用像素位图(CGImageRef)对图片进行处理

IOS中对图片进行重绘处理的方法总结 一.CGImageRef是什么 CGImageRef是定义在QuartzCore框架中的一个结构体指针,用C语言编写.在CGImage.h文件中,我们可以看到下面的定义: ? 1 typedef struct CGImage *CGImageRef; CGImageRef 和 struct CGImage * 是完全等价的.这个结构用来创建像素位图,可以通过操作存储的像素位来编辑图片. QuartzCore这个框架是可移植的. 二.CGImageRef相关的

iOS 中Block以及Blocks的使用

一.ios中block的使用 Block可以帮助我们组织独立的代码段,并提高复用性和可读性.iOS4在UIKit中引入了该特征.超过100个的Apple API都使用了Block,所以这是一个我们必须开始熟悉的知识. Block是什么样的? 你可以使用^操作符来声明一个Block变量,它表示一个Block的开始. int num1 = 7; int(^aBlock)(int) = ^(int num2) { return num1+nunm2; }; 在如上代码中我们将Block声明为一个变量,

深入了解iOS中的VC切换的传值方式

由于上次面试中有提到相关内容,所以这次我专门深入研究了iOS的几种方式: 首先把所有的传值方式都列出来,如果有遗漏,请指正 首先列出iOS中使用的传值方式: init 传值(即在创建VC的时候就对响应的参数进行设置) property 传值(即属性赋值) Router 传值(这个在OC中被使用,因为作者没有写Swift版本,所以先开个坑,估计我会填坑) Delegate 传值(通过协议和代理传值) 闭包(block)传值 (通过swift中的闭包,类似于OC中的block传值) Notifica

iOS中UIView之间布局及跳转的几种方式

UIView是iOS开发中所有视图的基类, 表示屏幕上的一块矩形区域, 同时可以处理该区域的绘制和触摸事件. UIViewController是视图控制器的基类, 用来处理屏幕之间的切换等操作, 提供视图管理模型. 一个UIViewController管理一个层级的UIView. 而RootViewController就是iOS应用启动时被载入的第一个视图控制器(可在main.storyboard中指定), 展示APP启动成功后的第一个界面. 因此, iOS中在各个UIViewControlle

iOS中的MVC

我们今天谈谈cocoa程序设计中的 模型-视图-控制器(MVC)范型.我们将从两大方面来讨论MVC: 什么是MVC? M.V.C之间的交流方式是什么样子的? 理解了MVC的概念,对cocoa程序开发是至关重要的. 一.MVC的概念 MVC是Model-VIew-Controller,就是模型-视图-控制器,这些都是什么东西呢? MVC把软件系统分为三个部分:Model,View,Controller.在cocoa中,你的程序中的每一个object(对象)都将明显地仅属于这三部分中的一个,而完全不

ios中创建控制器的几种方式

1.通过storyboard创建: (1)先加载storyboard文件: UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Test"  bundle:nil]; (2) 初始化storyboard中的控制器: ①初始化"初始控制器":HLViewController * hl = [storyboard instantiateInitialViewController]; ②通过标识初始