如何使用AlmofireImage最容易地将NSURLCache的内存容量设置为零?

时间:2016-07-09 21:07:43

标签: alamofire alamofireimage

我正在使用imageView.af_setImageWithURL(URL)并且效果很好。我没有对图像过滤器做任何事情,我看到了这个说明:

  

如果您不使用图像过滤器,建议将NSURLCache的内存容量设置为零,以避免在内存中存储相同的内容两次。

使用UIImage扩展实现此目的的最干净/最小的方法是什么?

2 个答案:

答案 0 :(得分:2)

你的答案肯定会奏效。这是另一种不需要您构建自己ImageDownloader的方法。

let downloader = UIImageView.af_sharedImageDownloader
let configuration = downloader.sessionManager.session.configuration
configuration.URLCache?.memoryCapacity = 0

这将最终做同样的事情,而不必创建任何新的实例。

答案 1 :(得分:0)

我正在尝试以下方法:

// From https://github.com/Alamofire/AlamofireImage - "If you do not use image filters, it is advised to set the memory capacity of the NSURLCache to zero to avoid storing the same content in-memory twice."
// Note: We're making an educated guess that NSURLCache does not allocate the memory capacity when it's created.. it just uses it as an upper bound. So we'll let ImageDownloader create the session configuration it wants, then we'll just set the memory capacity on its url cache to 0 as suggested.
let configurationWithNoMemoryCapacityOnURLCache = ImageDownloader.defaultURLSessionConfiguration()
configurationWithNoMemoryCapacityOnURLCache.URLCache?.memoryCapacity = 0

UIImageView.af_sharedImageDownloader = ImageDownloader(
    configuration: configurationWithNoMemoryCapacityOnURLCache
)