将 AlamofireImage 响应缓存到磁盘
Posted
技术标签:
【中文标题】将 AlamofireImage 响应缓存到磁盘【英文标题】:Caching AlamofireImage responses to disk 【发布时间】:2017-08-11 13:30:04 【问题描述】:我需要一些关于 AlamofireImages 的帮助。在应用程序中,点击表格视图单元格将使用内置的 ImageView 扩展程序下载并设置图像,如下所示:
func sendImageRequest(imageView: UIImageView, item: CatalogItem, isLargeImage: Bool? = false)
let imageURL = ImageManager.URLBuilder(sku: item.sku, largeImage: isLargeImage)
// Clear the image so that recycled images are cleared.
imageView.image = nil
// Create the request and add the token string to the authorization header
var urlRequest = try! URLRequest(url: URL(string: imageURL)!)
urlRequest.setValue("Bearer \(ImageManager.getTokenString()!)", forHTTPHeaderField: "Authorization")
// If the token exists AND is VALID (not expired) - Get the image.
if ImageManager.checkIfTokenExists() && ImageManager.checkTokenIsValid()
debugPrint("Valid token already exists. Fetching image... TableViewExtension")
imageView.af_setImage(withURLRequest: urlRequest) response in
switch response.result
case .success(let image):
debugPrint(response)
debugPrint("SUCCESS")
break
case .failure(let error):
debugPrint("Error: \(error)")
debugPrint(response.response)
break
// Else if the token exists AND is INVALID (expired) - Delete the old token, get a new one, and fetch the image
else if ImageManager.checkIfTokenExists() && !ImageManager.checkTokenIsValid()
debugPrint("Token expired... Getting new token.")
ImageManager.deleteToken()
let tokenRequest = NetRequest.newTokenRequest(url: "http://\(SettingsManager.KEY_ServerURL).ziizii.io/zz/jwt/new?service=images.ziizii.io")
tokenRequest.requestJWTToken()
debugPrint("Token renewed. Fetching image...")
imageView.af_setImage(withURLRequest: urlRequest)
// If the token doesn't exist, request a new one and fetch the image.
else
debugPrint("Requesting new token...")
let tokenRequest = NetRequest.newTokenRequest(url: "http://\(SettingsManager.KEY_ServerURL).ziizii.io/zz/jwt/new?service=images.ziizii.io")
tokenRequest.requestJWTToken()
debugPrint("Token aquired. Fetching image...")
imageView.af_setImage(withURLRequest: urlRequest)
所以,这部分我没问题。它可以工作,它将对象缓存到磁盘并设置 ImageView 图像。如果您稍后再次点击它,它会检查图像是否在缓存中,如果是,则简单地应用图像。整洁。
现在,该项目的一个新要求是让用户能够预加载每个图像并缓存它们。现在我不再使用 AFI ImageView 扩展,而是使用 Downloader。我不确定如何让下载器: A)缓存到磁盘和 B) 当用户点击一个单元格时,让 ImageView 扩展检查相同的缓存以查看它是否已预加载并应用该图像。
AFI 足够聪明,可以在发出请求之前检查缓存中的图像数据,所以我确信这是可能的。
我目前下载的图片如下:
static var downloader = ImageDownloader.default
static func downloadImage(urlRequest: URLRequestConvertible)
self.downloader.download(urlRequest) response in
switch response.result
case .success(let image): // If successful, cache the image.
print("SUCCESS")
break
case .failure(let error): // If unsuccessful, remove the cached 404 response and apply a nil image.
print("ERROR: \(response.error)")
break
如果有人知道如何完成此操作或任何有用的提示或技巧,那就太好了!谢谢!
【问题讨论】:
【参考方案1】:好吧,我假设您正在从字符串中下载图像作为下载 url。我处理过同样的问题;就我而言,我没有使用 Alamofire,我使用的是图像视图的扩展:
let imageCache = NSCache<AnyObject, AnyObject>()
扩展 UIImageView
func downloadImage(from imgURL: String!)
self.image = nil
// Check if there's an image in the cache
if let cachedImage = imageCache.object(forKey: imgURL as AnyObject) as? UIImage
self.image = cachedImage
return
// Otherwise, download image from Firebase via URL-string
let url = URLRequest(url: URL(string: imgURL)!)
let task = URLSession.shared.dataTask(with: url)
(data, response, error) in
if error != nil
print(error!)
return
DispatchQueue.main.async
if let image = UIImage(data: data!)
imageCache.setObject(image, forKey: imgURL as AnyObject)
self.image = UIImage(data: data!)
task.resume()
现在我通过调用解决了预加载问题
for i in objects // whatever object you're having
imageView.downloadImage(fromURL: i.url)
...在 viewDidLoad() 中。
现在它遍历所有自定义对象,下载并缓存所有图像,如果用户点击图像,它会立即从缓存中加载。我希望这可以帮助您解决问题。
【讨论】:
NSCache 不是缓存到磁盘,而是缓存到内存。问题是关于缓存到磁盘。以上是关于将 AlamofireImage 响应缓存到磁盘的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 中使用 AlamofireImage 将 GIF 图像转换为 UIImageView