IOS基础学习笔记二:Bundle加载文件、XIB使用

示例代码来自----黑马视频教程

1.加载文件

   //获取mainBundle

    NSBundle*bundle=[NSBundle mainBundle];

    // 获取images.plist在用户手机上的全路径

    NSString*path=[bundlepathForResource:@"images"ofType:@"plist"];

    //根据全路径

_imageData=[NSArrayarrayWithContentsOfFile:(path)];

    //NSLog(@"%@",_imageData);

  NSDictionary *dict=_imageData[0];

    _imageView.image=[UIImage imageNamed:dict[@"icon"]];


    _imageTitleView.text=dict[@"title”];


    //2.加载rou.xib文件 

    NSBundle * bundle=[NSBundle mainBundle];
    NSArray* objs=[bundle loadNibNamed:@"row" owner:self options:nil];

  需要设置XIB文件的File's Owner




3、UIImage的加载路径:

 1.[UIImage imageNamed:@"文件名"]

   * 有缓存,多次调用返回的是相同对象

   * 使用场合:图片使用频率高

 

 2.[[UIImage alloc] initWithContentsOfFile:@"全路径"];

   * 无缓存,每次调用返回的都是新对象

   * 使用场合:图片是一次性使用,用完这次,下次就不用了

   测试例子:所有图片(放入文件夹,后缀名改为.bundle),打包后放入项目中 为imges.bundle
    // 0.获取images.bundle的全路径,[NSBundle mainBundle]访问的是项目中的资源包
    NSString *path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"bundle"];
    
    // 1.加载images.bundle这个压缩包
    NSBundle *bundle = [[NSBundle alloc] initWithPath:path];
    //   2.获得某个图片
    NSString *imagePath = [bundle pathForResource:@"0" ofType:@"png"];
    // 3.获得images.bundle中所有png图片的全路径
    NSArray *paths = [bundle pathsForResourcesOfType:@"png" inDirectory:nil];

   //方法二:simageNamed
       self.imageView.image = [UIImage imageNamed:@"images.bundle/1.png"];





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章