您的位置:首页 > 移动开发 > IOS开发

iOS中bundle的意义

2015-08-04 18:11 519 查看
什么是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.");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: