gpt4 book ai didi

ios - Kingfisher 加载缓存图像

转载 作者:行者123 更新时间:2023-11-30 10:54:36 29 4
gpt4 key购买 nike

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

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

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

如何使用 kingfisher 正确实现这一目标

最佳答案

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

关于ios - Kingfisher 加载缓存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101473/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com