刷新 AlamofireImage 中的缓存图像?

Refresh cached image in AlamofireImage?

如果在服务器上更新了图像,是否可以自动删除缓存图像并下载更新的图像?我已经尝试过 AlamofireImage 和 STXImageCache,但它们都只下载一次图像并且不更新它。 我这样尝试:

private let downloader = ImageDownloader()

func downloadImage(path: String?, completion: @escaping (UIImage?) -> ()) {
    guard let path = path else { return }
    let urlRequest = URLRequest(url: URL(string: path)!)

    downloader.download(urlRequest) { response in
        completion(response.result.value)
    }
}

手动更新也不太好,因为如果我不知道服务器上是否更新了图像,我就必须完全忘记图像缓存。

看起来 none 可用 pods 能够在服务器上的内容更改时刷新缓存图像或任何其他缓存内容。由于Content-Control技术在技术上是可行的,但苹果似乎没有提供这种可能性https://developer.apple.com/documentation/foundation/nsurlrequest.cachepolicy/1414422-reloadrevalidatingcachedata

reloadRevalidatingCacheData Specifies that the existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source. This constant is unimplemented and shouldn’t be used.

你最好的办法是让服务器开发人员为任何会改变的图像(头像、背景、图标等)生成唯一的 URL 地址,或者如果服务器开发人员不可用,你应该干脆放弃有这种问题的地方缓存一下,随便下载。

在任何情况下,您都可以使用我的要点来下载缓存和未缓存的图像 https://gist.github.com/sam-moshenko/562ec61431c4a0ebeb68899b4d1b4d26(只是不要忘记安装 pod 'AlamofireImage'