0822-利用bundle归类资源文件

2016-08-22  本文已影响6人  JC_Wang

1、创建bundle(超简便,即修改文件夹后缀)

将要使用的资源文件放到一个文件夹内,比如图片、视频、JSON文件等
CDE1968A-5767-4886-AC89-AB73B3CA33A2.png

2、使用bundle的好处

      在一个程序中使用bundle,方便归类资源文件,图片放到一个bundle,JSON数据放到另外一个bundle,取数据的时候直接找到对应bundle,然后再找到对应的数据

3、如何使用


Demo:

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:imgView];
    NSString *bundlePath = [[NSBundle mainBundle]pathForResource:@"SRC" ofType:@"bundle"];
    NSBundle *resource = [NSBundle bundleWithPath:bundlePath];
    imgView.image = [UIImage imageNamed:@"321" inBundle:resource compatibleWithTraitCollection:nil];

或者

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:imgView];
    imgView.image = [UIImage imageNamed:@"SRC.bundle/321"];

4、注意事项

一定要先读取指定bundle,才能取到数据

上一篇 下一篇

猜你喜欢

热点阅读