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;
}
目录
相关文章
|
JSON 测试技术 数据处理
iOS-底层原理 35:组件化(一)方案
iOS-底层原理 35:组件化(一)方案
1131 0
iOS-底层原理 35:组件化(一)方案
|
开发工具 git 缓存
Git忽略规则.gitignore不生效
在项目开发过程中个,一般都会添加 .gitignore 文件,规则很简单,但有时会发现,规则不生效。 原因是 .gitignore 只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
54129 4
|
10月前
|
iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(二)(1)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(二)
112 0
|
12月前
|
iOS开发
iOS 调整leftBarButtonItem 的位置
iOS 调整leftBarButtonItem 的位置
101 0
|
iOS开发
iOS 系统自带分享功能
iOS 系统自带分享功能
667 0
|
开发框架 JavaScript 前端开发
理解iOS端的WebView同层组件
同层组件的目标是将原生组件渲染在与其他Web组件同一层级中。在iOS中,我们使用WKWebView来创建Web视图,WKWebView在进行解析渲染时,会将Web组件渲染到WKCompositingView上,这个View是一个原生的UIView子类,通常WKWebView内核会将多个组件共同渲染到同一个WKCompositingView上,但是如果某个HTML标签的style设置了overflow: scroll属性,并且内容超出容器的大小,WKWebView就会为其单独的创建一个WKChildScrollView,因此如果我们可以找到这个View,并和对应的Web组件一一关联起来,就可以将
1263 0
|
iOS开发 开发者
iOS iPhone手机弱网环境配置
iOS iPhone手机弱网环境配置
iOS iPhone手机弱网环境配置
|
C语言
『C语言』深度走入取整 & 4种函数
⒈trunc - 0向取整 ⒉floor -地板取整 ⒊ceil-无穷大取整 ⒋round-四舍五入
188 0
『C语言』深度走入取整 & 4种函数
|
编解码 Swift iOS开发
iOS 应用之间的跳转(OC&Swift)
iOS 应用之间的跳转(OC&Swift)
909 0
iOS 应用之间的跳转(OC&Swift)