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"];





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