gpt4 book ai didi

swift - 使用 Kingfisher 时重新下载不同尺寸的图像

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

我有一个可以使用 API JSON 响应的应用程序。只是检索一个简单的项目数组。每个项目都有一个带有高分辨率图像的 URL。我正在使用 Kingfisher 获取此图像并将其大小调整为我的 ImageView 大小(放置在我的 MainView 上)

//MainViewController
itemImageMainVC.kf.setImage(
with: URL(string: itemModel.artImage),
options: [
.processor(DownsamplingImageProcessor(size: itemImageMainVC.bounds.size) >> RoundCornerImageProcessor(cornerRadius: 16, targetSize: itemImageMainVC.bounds.size)),
.scaleFactor(UIScreen.main.scale),
.transition(.fade(0.2)),
.cacheOriginalImage
])

这里一切都很好,我的图像位于 TableView 内的 MainView 上,并且它们缓存得很好。但!如果我尝试打开并显示相同的图像,但在 SecondView 上具有不同的尺寸,我会在 SecondView 上看到相同的过渡动画,所以我猜它们在不应该的情况下开始再次下载。

//SecondViewController
itemImageSecondVC.kf.setImage(
with: URL(string: itemModel.artImage),
options: [
.processor(DownsamplingImageProcessor(size: itemImageSecondVC.bounds.size) >> RoundCornerImageProcessor(cornerRadius: 16, targetSize: itemImageSecondVC.bounds.size)),
.scaleFactor(UIScreen.main.scale),
.transition(.fade(0.2)),
.cacheOriginalImage
])

itemImageMainVC 和 itemImageSecondVC 具有不同的大小。

我做错了什么?我只想在 MainView 上下载/显示图像,并在打开 SecondView 时立即显示它们,而无需重新下载

最佳答案

让我们尝试使用cacheKey,它应该是图像网址中的最后一个路径组件(或事件absoluteUrlString)或唯一的东西。

let resource = ImageResource(downloadURL: url, cacheKey: url.lastPathComponent)
self.imageView.kf.setImage(with: resource, placeholder: UIImage(named: "placeholder-image"), options: nil, progressBlock: nil) { (result) in
switch result {
case .success(_):
// Success case
case .failure(_):
// Failed case
}
}

关于swift - 使用 Kingfisher 时重新下载不同尺寸的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967061/

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