翠鸟负载缓存图像

时间:2019-01-09 00:00:53

标签: ios swift kingfisher

我的API返回两种类型的照片:小照片和大照片

我使用翠鸟来加载照片,并且总是先加载小照片。当我加载大照片时,我想实现这一目标

if hasCacheSmallPhoto {
  imageView.kf.setImage(with: bigImageUrl, placeholder: cacheImage)
} else {
  imageView.kf.setImage(with: bigImageUrl, placeholder: defaultPlaceHolderImage)
}

我如何才能使用翠鸟实现这一目标

1 个答案:

答案 0 :(得分:0)

您需要首先检查kingFisher缓存是否缓存了小图像。

    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
    }
}
}