ios - 刷新 AlamofireImage 中的缓存图像?

标签 ios swift caching alamofire image-caching

如果图像在服务器上更新,是否可以自动删除缓存的图像并下载更新的图像?我尝试过 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)
    }
}

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

最佳答案

当服务器上的内容发生更改时,似乎没有可用的 Pod 能够刷新缓存的图像或任何其他缓存的内容。由于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')

关于ios - 刷新 AlamofireImage 中的缓存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46069102/

相关文章:

asp.net - 缓存在它应该到期之前到期

asp.net - 如何避免 Global.asax 进行 'VaryByCustom' 缓存

ios - 关闭应用程序时 webview 的 cookie 会发生什么变化?

ios - 通过 AdMob 展示的广告很少

ios - 如何对工具栏项应用约束

ios - CanOpenUrl 方法不适用于 ios 9

javascript - React Native iOS 构建错误 - 检查类的渲染方法

ios - NS_ASSUME_NONNULL_END 之后的可空性注释警告

SwiftUI 自定义列表样式

java - Guava 缓存和保留检查的异常