[iOS]加载图片的那些事

2016-02-02  本文已影响246人  PlutoMa

在iOS开发中,我们常用的从程序中加载图片的方法有两种,分别是:

+ (nullable UIImage *)imageNamed:(NSString *)name;

+ (nullable UIImage *)imageWithContentsOfFile:(NSString *)path;

先说这二者的区别:

再说一下这两个方法在适配时的一些注意点:

    [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"1.png"];
[[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];

在这里要注意,第一种获取路径的方法会正确的根据机型获取到图片,但是第二种方法要的是绝对名称,所以第二种方法得到的图片是nil,必须按下面方法才能得到图片:

[[NSBundle mainBundle] pathForResource:@"1@2x" ofType:@"png"];
上一篇 下一篇

猜你喜欢

热点阅读