zoukankan      html  css  js  c++  java
  • 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.");

  • 相关阅读:
    Maven POM元素继承
    Maven模块聚合
    Maven生命周期小记
    maven配置远程仓库
    Maven把自己的包部署到远程仓库
    java基础知识之一:命名规则(包名、类名、变量名、方法名)
    httpclient+maven+excel+testng 框架实例----02 之代码详解 之配置报告输出目录
    httpclient+maven+excel+testng 框架实例----02 之代码详解
    httpclient+maven+excel+testng 框架实例----02 之项目目录结构及相关配置文件
    httpclient+maven+excel+testng 框架实例----02 之配置pom文件
  • 原文地址:https://www.cnblogs.com/zjoch/p/4702745.html
Copyright © 2011-2022 走看看