0

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

我使用翠鸟加载照片,我总是先加载小照片。当我加载大照片时,我想实现这一点

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

我怎样才能正确地使用翠鸟实现这一目标

4

1 回答 1

0

您需要先检查 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
    }
}
}
于 2019-01-09T13:36:14.620 回答