【问题标题】:Swift, Kingfisher Cache not working properly?Swift,翠鸟缓存无法正常工作?
【发布时间】:2019-09-01 09:04:27
【问题描述】:

我正在尝试使用 Cocoapod KingFisher 缓存图像,我使用的代码确实显示了来自数据库存储的图像,但它没有缓存。我很想知道为什么?

打印总是说“缓存结果无”。而且我还注意到图像没有被缓存。

调用imageDownloader的代码:

DownloadImage(imageId : nextUser.id, cardImage: secondProfilePic)

用于下载和缓存的代码,也用于检查是否缓存。

func DownloadImage(imageId : String, cardImage : UIImageView){

        let imagesStorageRef = Storage.storage().reference().child("profilepic/").child(imageId)
        //Get URL For Cache
        imagesStorageRef.downloadURL { url, error in
            if let error = error {
                // Handle any errors
                cardImage.image = UIImage(named: "RentOutProfilePic")
                print("Error")
            } else {
                // Get the download URL for '.jpg'
                let pathURL = url
                print("Sets Image")
                cardImage.kf.indicatorType = .activity
                cardImage.kf.setImage(with: pathURL,
                                                  options: [
                                                    .transition(.fade(0.3)),
                                                    .cacheOriginalImage
                    ])
            }
            if let url = url{
                let tempUrl:String = url.path
                let cache = ImageCache.default
                let cached = cache.imageCachedType(forKey: tempUrl)
                print("cache Result \(cached)")
            }

        }

    }

【问题讨论】:

  • 非常感谢您对此问题的任何帮助,谢谢!

标签: swift image caching kingfisher


【解决方案1】:
  1. Kingfisher 默认使用url.absoluteString 作为图像的缓存键。因此,在您的代码中,url.path 将始终返回“未缓存”的结果。

  2. 您正试图在设置图像的同时打印缓存结果。第一次,您的图像将处于下载过程中,因此即使您根据 1 正确设置了密钥,您也总是得到.none。在以下使用相同 id 调用此方法时,您应该得到一个缓存结果作为磁盘或记忆。

  3. 我不确定您是如何得出“图像未缓存”的结论的。 Kingfisher 默认基于 url 做缓存。如果您每次调用图像视图设置方法时都有不同的 url(从您的imagesStorageRef 返回),则不会有匹配的缓存并会发生下载。如果是这种情况,您可以自定义使用 imageId 作为缓存键。为此,您需要指定另一个缓存键。请参阅此wiki section 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2019-11-24
    • 2013-03-04
    • 1970-01-01
    相关资源
    最近更新 更多