本文介绍了UIImage 是否缓存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIImage *img = [[UIImage alloc] initWithContentsOfFile:@"xx.jpg"]
UIImage *img = [UIImage imageNamed:@"xx.jpg"]

在第二种类型中,图像会被缓存吗?
而在第一种类型中,图像不会被缓存?

In the second type will the image get cached ?
Whereas the in the first type the images doesn't get cached?

推荐答案

  • -initWithContentsOfFile: 创建一个没有缓存的新图像,这是一个普通的初始化方法.

    • The -initWithContentsOfFile: creates a new image without caching, it's an ordinary initialization method.

      +imageNamed: 方法使用缓存.这是来自 UIImage Reference 的文档:

      The +imageNamed: method uses cache. Here's a documentation from UIImage Reference:

      此方法在系统缓存中查找具有指定名称的图像对象,如果存在则返回该对象.如果缓存中没有匹配的图像对象,则此方法从指定文件加载图像数据,缓存它,然后返回结果对象.

      UIImage 将保留加载的图像,使其保持活动状态直到内存不足导致缓存被清除.

      UIImage will retain loaded image, keeping it alive until low memory condition will cause the cache to be purged.

      Swift 更新:在 Swift 中,UIImage(named: "...") 函数是缓存图像的函数.

      Update for Swift:In Swift the UIImage(named: "...") function is the one that caches the image.

      这篇关于UIImage 是否缓存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 09:33
查看更多