【问题标题】:AlamofireImage cache?AlamofireImage 缓存?
【发布时间】:2016-08-10 07:51:17
【问题描述】:

我将AlamofireImagePromiseKitAlamofire 结合使用。我使用 Promise 链接下载 x 个图像,因为我想同步执行此操作。我似乎无法理解ImageDownloader() 是自动缓存还是必须将图像显式添加到缓存中?到目前为止,我看到的唯一示例不是使用ImageDownloader,所以我很难找到答案。

如果没有 - 我如何将其添加到缓存中?我试过使用:

self.imageDownloader.imageCache?.addImage(image, withIdentifier: imageUrl)

但它所做的只是永远增加我的内存使用量(即一遍又一遍地将相同的图像添加到缓存中)

【问题讨论】:

    标签: ios swift alamofire alamofireimage


    【解决方案1】:

    我认为定义一个 AutoPurgingImageCache() 然后在缓存过程中使用它需要解决你的内存使用问题。

    let photoCache = AutoPurgingImageCache(
        memoryCapacity: 100 * 1024 * 1024,
        preferredMemoryUsageAfterPurge: 60 * 1024 * 1024
    )
    

    你可以改变memoryCapacitypreferredMemoryUsageAfterPurge代表MB。要在缓存中添加任何图像,您可以这样使用:

    photoCache.addImage(scaledImage, withIdentifier: urlString)
    

    您也可以查看this topic 了解更多详情和AlamofireImage GitHub 页面。

    【讨论】:

    • 那里有一个小的语法错误。要将图像添加到缓存中,您需要调用 .add 而不是 .addImage
    【解决方案2】:

    我也使用了与您在这里相同的方法。但根据 API 参考,图像应自动添加到缓存中:

    Each downloaded image is cached in the underlying NSURLCache as well as the in-memory image cache that supports image filters.

    https://alamofire.github.io/AlamofireImage/Classes/ImageDownloader.html

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 2016-02-01
      • 2017-02-18
      • 2017-03-10
      • 2018-02-14
      • 1970-01-01
      • 2016-08-01
      • 2017-09-18
      • 2016-09-30
      相关资源
      最近更新 更多