iOS引用图片文件的三种方式

来源:互联网 发布:广源良 知乎 编辑:程序博客网 时间:2024/06/06 10:21
1.folder references:
<span style="font-size:14px;">NSString * path = @"imgSource/zjload.jpg";NSString *newPath=[NSString stringWithFormat:@"%@%@%@",[[NSBundle mainBundle]resourcePath],@"/",path];NSLog(@"newPath===%@",newPath);UIImageView * img = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:newPath]];</span>
好处:
  1. 直接对资源文件和目录结构进行调整而不需要修改项目文件
  2. 资源文件可以重名
  3. 如果有不同的target 每个target维护不同的根资源文件夹就行了

2.Images.xcassets:

(1)只支持png格式的图片

(2) 图片只支持[UIImage imageNamed]的方式实例化,但是不能从Bundle中加载

(3)  在编译时,Images.xcassets中的所有文件会被打包为Assets.car的文件

3.group:
可以直接使用[NSBundle mainBundle]作为资源路径,效率高!
<span style="font-size:14px;">NSBundle *mainBundle = [NSBundle mainBundle];NSString *imagePath = [mainBundle pathForResource:@"aboutTitle@2x" ofType:@"png"];[UIImage imageWithContentsOfFile:imagePath];</span>

可以使用[UIImage imageNamed:]加载图像
0 0