iOS中bundle的意义

本文介绍了 iOS 开发中 bundle 的概念及其重要性。详细解释了 bundle 是如何组织资源的,它与普通文件夹的区别,以及如何利用 bundle 实现应用的国际化。此外,还提供了访问 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.");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值