我有一个定制的UICollectionViewCell,它使用AlamofireImage (2.4)获取图像。它可以下载,但它似乎并没有放在缓存中。
我的全局缓存中有一个快速文件。
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加载这个单元格时,它只会再次下载图像。知道为什么这不管用吗?
发布于 2016-11-28 15:33:54
我发现我只需要使缓存的大小更大。我下载的图片比我预计的要大,缓存的填充速度太快,然后清除。
// 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)')https://stackoverflow.com/questions/38352328
复制相似问题