iOS

笔记@Bundle Programming Guide

2017-05-19  本文已影响300人  fever105

笔记@Bundle Programming Guide


概览

A bundle is a directory with a standardized hierarchical structure that holds executable code and the
resources used by that code.

Bundle和Package

A package is any directory that the Finder presents to the user as if it were a single file.

Bundle的类型
Bundle的创建

Bundle结构

iOS Bundle结构
app bundle /
    可执行文件
    Info.plist
    app icon(如有)
    app启动图片(如有)
    storyboard / nib(如有)
    Settins.bundle(用于在系统设置中显示相关内容,如有)
    通用资源文件(同本地化资源相对,如音频,视频,数据库文件)
    en.lproj...(本地化资源目录)
framework结构

The structure for frameworks is based on a bundle format that predates OS X and supports the inclusion of multiple versions of the framework’s code and resources in the framework bundle.

MyFramework.framework/
    MyFramework->Versions/Current/MyFramework // 指向最新版本
    Resources->Versions/Current/Resources // 指向最新版本
    Versions/
        Current->A // 指向最新版本
        A/
            MyFramework
            Info.plist
            Headers/
                MyHeaders.h
            Resources/
                English.lproj/
                    InfoPlist.strings

The bundle’s Versions subdirectory contains the individual framework revisions while symbolic links at the top of the bundle directory point to the latest revision.

Loadable Bundle
MyLoadableBundle.bundle
    Contents/
        Info.plist
        MacOS/
            MyLoadableBundle // 可执行文件,资源包没有这个目录
        Resources/
            Pic.jpg
            Beauty.png
            en.lproj/
                MyLoadableBundle.nib
                InfoPlist.strings
            jp.lproj/
                MyLoadableBundle.nib
                InfoPlist.strings

Document Package

访问Bundle内容

Bundle对象

The main bundle is the bundle that contains the code and resources for the running application.

资源搜索模式

系统在bundle中搜索资源时,分为两步:

  1. 确定使用哪个本地化文件夹内的资源:

    • 根据系统的首选语言顺序寻找相应地本地化文件夹?.lproj
    • 如果没找到,则根据CFBundleDevelopmentRegion来确定选定本地化文件夹。
  2. 然后按照以下优先级搜索:

    1. 通用资源目录
    2. 特定地区资源目录
    3. 特定语言资源目录
    4. 开发语言资源目录
根据iOS设备类型区分资源

从其他Bundle中加载资源

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1. 在main bundle中找到特定bundle
    NSString *sampleBundlePath = [[NSBundle mainBundle] pathForResource:@"SampleBundle.bundle" ofType:nil];
    // 2. 载入bundle,即创建bundle对象
    NSBundle *sampleBundle = [NSBundle bundleWithPath:sampleBundlePath];
    // 3. 从bundle中获取资源路径
    // 注意这里的图片位于通用资源目录下的Images二级目录,相对路径要明确这种关系
    NSString *pic1Path = [sampleBundle pathForResource:@"pic1.png" ofType:nil];
    // 4. 通过路径创建对象
    UIImage *image = [UIImage imageWithContentsOfFile:pic1Path];
    
}
如下代码从app bundle根目录下的另一个bundle中获取一张图片

通用资源目录


拾遗

If you used custom subdirectories in your bundle to organize resource files, you can speed up the search by providing the name of the subdirectory that contains the desired file.

上一篇下一篇

猜你喜欢

热点阅读