您需要先检查 kingFisher Cache 是否缓存了小图像。
if ImageCache.default.isCached(forKey:smallImageUrl) {
ImageCache.default.retrieveImage(forKey: smallImageUrl) { result in
switch result {
case .success(let value):
imageView.kf.setImage(with: bigImageUrl, placeholder: value.image)
case .failure(let error):
print(error)
}
}
}
else {
imageView.image = defaultPlaceHolderImage
imageView.kf.setImage(with: smallImageUrl) { result in
// `result` is either a `.success(RetrieveImageResult)` or a `.failure(KingfisherError)`
switch result {
case .success(let value):
// The image was set to image view:
imageView.kf.setImage(with: bigImageUrl, placeholder: value.image)
// The source object which contains information like `url`.
print(value.source)
case .failure(let error):
print(error) // The error happens
}
}
}