繁体   English   中英

翠鸟负载缓存图像

[英]Kingfisher load cache image

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

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

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

如何使用翠鸟正确实现

您需要先检查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
    }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM