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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM