首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlamofireImage集合单元下载后将图像放入AutoPurgingImageCache中

AlamofireImage集合单元下载后将图像放入AutoPurgingImageCache中
EN

Stack Overflow用户
提问于 2016-07-13 12:52:15
回答 1查看 490关注 0票数 3

我有一个定制的UICollectionViewCell,它使用AlamofireImage (2.4)获取图像。它可以下载,但它似乎并没有放在缓存中。

我的全局缓存中有一个快速文件。

代码语言:javascript
复制
import Foundation
import Alamofire
import AlamofireImage

// AlamofireImage cache with 30 MB cache
let imageCache = AutoPurgingImageCache(
    memoryCapacity: 30 * 1024 * 1024,
    preferredMemoryUsageAfterPurge: 20 * 1024 * 1024
)

集合单元看起来是这样的。它用af_setImageWithURL下载映像,并在回调中将该映像放在全局缓存中,然后将其设置在单元上。

代码语言:javascript
复制
import UIKit
import Alamofire
import AlamofireImage

    class MyCatCell: UICollectionViewCell {

    @IBOutlet weak var fancyImage: UIImageView!

    var imageURL: String?

    func configureCell(uri: String?) {
        imageURL = uri
        resetCell()
        loadImage()
    }

    func resetCell() {
        fancyImage.af_cancelImageRequest()
        fancyImage.image = nil
    }

    func loadImage() {
        guard imageURL != nil else {
            return
        }
        if let image = imageCache.imageWithIdentifier(imageURL!) {
            // set image from cache
            print("image from cache")
            fancyImage.image = image
        } else {
            // get image and cache it
            let url = NSURL(string: imageURL!)!
            fancyImage.af_setImageWithURL(
                url,
                placeholderImage: nil,
                filter: nil,
                imageTransition: .CrossDissolve(0.5),
                completion: { response in
                    if response.result.isSuccess {
                        print("response.result.isSuccess \(self.imageURL!)")
                        // store this in the image cache
                        imageCache.addImage(response.result.value!, withIdentifier: self.imageURL!)
                    }
                }
            )
        }
    }
}

图像设置在单元格中,print("response.result.isSuccess \(self.imageURL!)")触发(因此调用正在运行),但print("image from cache")从不触发。imageCache.imageWithIdentifier(imageURL!)总是nil。当我再次使用相同的URL加载这个单元格时,它只会再次下载图像。知道为什么这不管用吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-28 15:33:54

我发现我只需要使缓存的大小更大。我下载的图片比我预计的要大,缓存的填充速度太快,然后清除。

代码语言:javascript
复制
// AlamofireImage cache with 60 MB cache
let imageCache = AutoPurgingImageCache(
    memoryCapacity: 60 * 1024 * 1024,
    preferredMemoryUsageAfterPurge: 20 * 1024 * 1024
)

如果定期使用imageCache.memoryUsage.,您可以看到缓存已被填满。

代码语言:javascript
复制
Print('Image cache size = \(imageCache.memoryUsage)')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38352328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档