gpt4 book ai didi

swift - AlamofireImage Collection 单元在下载后将图像放入 AutoPurgingImageCache

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:26 24 4
gpt4 key购买 nike

我有一个自定义 UICollectionViewCell,它使用 AlamofireImage (2.4) 获取图像。下载正常,但似乎没有放入缓存。

我有一个带有全局缓存的 swift 文件。

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 下载图像,并在回调中将此图像设置到单元格上后将其放入全局缓存中。

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 再次加载此单元格时,它只是再次下载图像。知道为什么这不起作用吗?

最佳答案

我发现我只需要将缓存的大小变大即可。我正在下载的图像比我预期的要大,而且缓存填满的速度太快,然后就被清除了。

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

如果您定期使用 imageCache.memoryUsage,您会看到缓存已满。

Print('Image cache size = \(imageCache.memoryUsage)')

关于swift - AlamofireImage Collection 单元在下载后将图像放入 AutoPurgingImageCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38352328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com