使用 Kingfisher ios 在同一 url 中显示更新的图像

Show updated images in the same url using Kingfisher ios

我正在使用 Kingfisher 加载来自 Url 的图像视图。有时,相同的 url 会更新为新图像。 所以我使用下面的代码来加载图像视图,

profileImage.kf.setImage(with: profileUrl, placeholder: #imageLiteral(resourceName: "profile_1"), options: [.fromMemoryCacheOrRefresh], progressBlock: nil, completionHandler: nil)

但是图像没有得到更新。仅显示旧图像。 为什么会这样?在 Kingfisher 文档中指出,“fromMemoryCacheOrRefresh 可用于在相同 url 后面显示可变图像,同时避免一次又一次地下载它”

翠鸟不支持服务器缓存机制。它只是使用整个 URL 作为本地缓存键。只要你使用相同的URL,你就会从缓存中得到相同的图像(如果它被缓存)。

因此,如果您的服务器在相同 URL 下提供不同的图像,我们建议您让您的服务器在 URL 中添加查询以获取不同的版本。这些 URLs:大多数服务器实现的“https://example.com/image.png?v=1" and "https://example.com/image.png?v=2" represent different images in Kingfisher and it could work well. At the same time, access to the "https://example.com/image.png?v=2" would navigate you to the "https://example.com/image.png”数据。

您可以在此页面上找到更多信息:

https://github.com/onevcat/Kingfisher/wiki/FAQ#does-kingfisher-support-server-cache-tags-like-e-tag-or-last-modified

我使用了以下方法来解决这个问题。通过这样做,它会将缓存的图像设置为占位符,并且仅在有任何更改时才替换图像,并且更改不会像在后台完成的那样明显。如果您需要任何进一步的帮助,请告诉我。 Note:here 我正在设置按钮的图像

                    self.avatar.kf.setBackgroundImage(with: URL(string: imagelink), for: UIControl.State()){
                        result in
                        switch result{
                        case .success(let value): 
                        self.avatar.kf.setBackgroundImage(with: URL(string: imagelink), for: UIControl.State(), placeholder: value.image,options: [.forceRefresh])
                        print("avatar set")
                            break
                        case .failure: print("not found in cache")
                            break
                        }