imageNamed与imageWithContentsOfFile小结

简介: pexels-photo-892769.jpeg本文主要讲imageNamed与imageWithContentsOfFile的差异,需要注意的点,与实战中遇到的坑。
img_7dc8cb33073aadf10d3c3fce590d78df.jpe
pexels-photo-892769.jpeg

本文主要讲imageNamed与imageWithContentsOfFile的差异,需要注意的点,与实战中遇到的坑。

好久没写过博客了,什么工作太忙,加班太晚我就不说了,都怪自己太懒,时间都是挤出来的。看着各位大牛写的文章,简直过瘾,希望有一天自己也能写出这么高质量、干货密集的文章,先从简单的做起吧。

从差异说起

从磁盘加载图片,UIImage主要提供了两种方式:

+(UIImage *)imageNamed:(NSString *)name;
+(UIImage *)imageWithContentsOfFile:(NSString *)path;

关于这两种方法的使用时机,苹果官方文档描述如下:

Use the imageNamed:inBundle:compatibleWithTraitCollection: method (or the imageNamed: method) to create an image from an image asset or image file located in your app’s main bundle (or some other known bundle). Because these methods cache the image data automatically, they are especially recommended for images that you use frequently.
Use the imageWithContentsOfFile: or initWithContentsOfFile: method to create an image object where the initial data is not in a bundle. These methods load the image data from disk each time, so you should not use them to load the same image repeatedly.

也就是说,imageNamed:第一次加载图片时会缓存图片到内存,适合使用频繁的图片,imageWithContentsOfFile:不会把图片缓存到内存,每次调用都要重新从磁盘加载一次。
在实际使用中我们要根据业务来判断调用具体的方法,来最优化内存与性能。举个例子:

  1. 登陆背景图,只会在用户登陆的时候使用,而且图片较大,就建议用imageWithContentsOfFile:加载;
  2. 底导航图标,图标较小,使用频繁,就建议使用imageNamed:加载;

imageNamed:方法还有个限制,它是在main bundle里找图片,如果图片放在Images.xcassets或者直接把图片方在工程里,参数直接传图片名可以找到。像我司的图片是放在单独建立的bundle里,如果要用imageNamed:加载的话文件名前面就要加上bundle名,像这样a.bundle/b.png

屏幕适配问题

iOS的图片文件需要提供3种尺寸的1x、2x、3x,根据不同的屏幕尺寸我们需要加载不同的图片,关于不同屏幕的图片加载,苹果已经帮我们封装好了,我们只需要将3中尺寸的图片放到工程中,然后调用imageNamed:或者imageWithContentsOfFile:,它会自动根据屏幕尺寸来加载不同的图片。
关于imageNamed:,官方文档中有这么一段讨论:

This method looks in the system caches for an image object with the specified name and returns the variant of that image that is best suited for the main screen.

imageWithContentsOfFile:还没找到官方文档的说明(如果各位知道,欢迎各位大牛在评论中提出),不过我测试过是可以的。

使用imageWithContentsOfFile的一个坑

在使用imageWithContentsOfFile:加载图片的时候遇到一个坑,先上代码:

+ (UIImage *)imageWithName:(NSString *)name type:(NSString *)type inBundle:(NSString *)bundle {
    NSString *imageBundlePath = [[NSBundle mainBundle] pathForResource:bundle ofType:@"bundle"];
    NSBundle *imageBundle = [NSBundle bundleWithPath:imageBundlePath];
    NSString *imagePath = [imageBundle pathForResource:name ofType:type];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    return image;
}

很简单的一个函数,就是获取bundle全路径,然后再获取到bundle里图片的全路径,然后调用imageWithContentsOfFile:加载图片。在使用的时候也很正常,但是有一天发现某张图加载不出来了。检查资源文件,只有2x的图(又是一个偷懒的程序员。。。很不建议这么玩,虽然只有2x的图,在所有屏幕都能显示,但是会造成图片的压缩与放大,每个细节都很重要!!!),如果加上1x的图就可以加载出来了。
经过调试发现问题就出在pathForResource:ofType上,这个函数是精确匹配调用者输入的文件名,不会自动识别文件名后面的@2x。修改后的代码:

+ (UIImage *)imageWithName:(NSString *)name type:(NSString *)type inBundle:(NSString *)bundle {
    NSString *imageBundlePath = [[NSBundle mainBundle] pathForResource:bundle ofType:@"bundle"];
    NSBundle *imageBundle = [NSBundle bundleWithPath:imageBundlePath];
    NSString *imageFullName = [name stringByAppendingPathExtension:type];
    NSString *imagePath = [[imageBundle resourcePath] stringByAppendingPathComponent:imageFullName];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    return image;
}
目录
相关文章
|
开发工具 git 缓存
Git忽略规则.gitignore不生效
在项目开发过程中个,一般都会添加 .gitignore 文件,规则很简单,但有时会发现,规则不生效。 原因是 .gitignore 只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
50166 4
|
10月前
|
C语言 C++ iOS开发
iOS中C++静态全局变量的动态初始化时序
一个由于C++初始化失败导致Realm初始化失败的Crash
139 1
|
开发框架 JavaScript 前端开发
理解iOS端的WebView同层组件
同层组件的目标是将原生组件渲染在与其他Web组件同一层级中。在iOS中,我们使用WKWebView来创建Web视图,WKWebView在进行解析渲染时,会将Web组件渲染到WKCompositingView上,这个View是一个原生的UIView子类,通常WKWebView内核会将多个组件共同渲染到同一个WKCompositingView上,但是如果某个HTML标签的style设置了overflow: scroll属性,并且内容超出容器的大小,WKWebView就会为其单独的创建一个WKChildScrollView,因此如果我们可以找到这个View,并和对应的Web组件一一关联起来,就可以将
1146 0
|
自然语言处理 编译器 Linux
【Linux篇】第十一篇——动静态库(动静态库的介绍+动静态库的打包与使用)
【Linux篇】第十一篇——动静态库(动静态库的介绍+动静态库的打包与使用)
【Linux篇】第十一篇——动静态库(动静态库的介绍+动静态库的打包与使用)
|
并行计算 算法 Swift
iOS 利用 Metal 实现滤镜与动效滤镜
iOS 利用 Metal 实现滤镜与动效滤镜
iOS 利用 Metal 实现滤镜与动效滤镜
|
JSON 前端开发 测试技术
benny 介绍 - 一个简单的 benchmark 框架
benny 是一个简单的 benchmark 框架,当你需要测试自己的库或是方法性能时,可使用它来进行对其进行基准测试。
|
网络协议 网络性能优化 流计算
RTSP协议、RTMP协议、HTTP协议的区别
理论上RTSP RTMPHTTP都可以做直播和点播,但一般做直播用RTSP RTMP,做点播用HTTP。做视频会议的时候原来用SIP协议,现在基本上被RTMP协议取代了。 RTSP、 RTMP、HTTP的共同点、区别 共同点: 1:RTSP RTMP HTTP都是在应用应用层。
3075 0
|
API iOS开发 算法
IOS 30多个iOS常用动画,带详细注释
<div class="bar" style="padding-left:45px; font-family:Consolas,'Courier New',Courier,mono,serif; line-height:26px; background-color:rgb(231,229,220)"> <div class="tools" style="padding:3px 8px 1
2327 0
|
Android开发 iOS开发
iOS配置证书:Provisioning profile "xx_Distribution" doesn't include signing certificate "iPhone Developer
iOS配置证书:Provisioning profile "xx_Distribution" doesn't include signing certificate "iPhone Developer
462 0