【问题标题】:Re-downloading images with different sizes when using Kingfisher使用 Kingfisher 时重新下载不同大小的图像
【发布时间】:2019-09-17 03:45:03
【问题描述】:

我有一个适用于 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 时立即显示它们,而无需重新下载

【问题讨论】:

    标签: swift caching kingfisher


    【解决方案1】:

    让我们尝试使用 cacheKey,它应该是图像 url 中的最后一个路径组件(或事件 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
        }
    }
    

    【讨论】:

    • 问题出在 RoundCorner 处理器上。因为我要求一张不同尺寸的图片
    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2021-07-11
    相关资源
    最近更新 更多