Bundle in Ios~

来源:互联网 发布:年均增速算法 编辑:程序博客网 时间:2024/05/17 14:18

Bundle简单地讲,就是一个内部结构按照标准规则组织的特殊目录。

用途主要是软件的国际化,类似如java .property等一些文件的作用。

场景:软件有美国、中国版本,软件的logo等图片不同,这时候,我们把相应的图片资源放到一个文件夹下,然后修改文件夹的名字.bundle,然后添加到xcode中。

我们知道每个project都有个mainBundle,那如何去获取自己定义的bundle内容呢?

我们把自定义的bundle当成项目的一种资源,由mainBundle去获取~,然后我们用相同的方式获取自定义的bundle下的资源。

过程:

1、通过mainBundle去加载自定义的bundle

2、通过获取到的自定义bundle去获取资源

在这里,我们根据上述场景来写个demo。

项目的结构如下:


注:America.bundle和China.bundle分别代表不同软件版本的资源包。

在AvatarImage.xib中我们会根据软件版本显示不同的图片。

AvatarImage.m部分代码:

#pragma mark 根据国家来选择图片展示

- (void)showImages:(NSString *)country

{

    if (nil != country && country.length > 0)

    {

        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:country ofType:@"bundle"];

        if (resourcePath.length > 0)

        {

            NSBundle *countryBundle = [[NSBundlealloc] initWithPath:resourcePath];

            NSString *avatarPath = [countryBundle pathForResource:@"avatar"ofType:@"png"]; 

            if (avatarPath.length > 0)

            {

                UIImage *image = [UIImage imageWithContentsOfFile:avatarPath];

                [avatarImage setImage:image];

            }

            else

            {

                NSLog(@"The avatar image not found~~");

            }

        }

        else

        {

            NSLog(@"The country resource not found~~");

        }

    }

}


#pragma mark - View lifecycle


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    [self showImages:@"America"];

}

America的view截图:


China的截图:

 

抛砖引玉~~记录下自己学的


出自:http://avatar-matrix.lofter.com/post/e4689_236e82

0 0
原创粉丝点击