AlamofireImage磁盘缓存不起作用

编程入门 行业动态 更新时间:2024-10-22 18:30:52
本文介绍了AlamofireImage磁盘缓存不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用AlamofireImage从URL下载图像,然后将其缓存在设备磁盘空间上。我阅读了有关此内容的几篇文章,发现AlamofireImage在 ImageDownloader 类。在此SO答案中,

I want to use AlamofireImage to download images from an url and then cache it on the device disk space. I read several posts about this and found out that AlamofireImage supports this with the help of the ImageDownloader class. The most interesting information was given in this SO answer

所以我尝试为ImageDownloader设置自定义NSURLCache,以便它将图像直接缓存到磁盘。我通过将内存容量设置为0来做到这一点。然后使用此自定义ImageDownloader下载图像。我为磁盘路径指定的文件夹是在磁盘上创建的,但不幸的是,该文件夹保持为空,并且图像没有以任何方式进行缓存。

So I tried to set a custom NSURLCache for the ImageDownloader so that it would cache the images directly to the disk. I did that by setting the memory capacity to 0. Then I use this custom ImageDownloader to download an image. The folder that I specified for the disk path is created on the disk but unfortunately it stays empty and the image is not cached in any way.

**编辑:**重要的是要注意,缓存的响应不是保存在缓存目录的文件夹中,而是保存在该文件夹旁边的数据库文件中。

** ** It is important to notice that the cached responses are not saved in the folder in the caches directory but in a database file next to the folder.

有人可以告诉我我这里做错了吗?非常感谢您的阅读!

Can anybody tell me what I am doing wrong here? Thanks very much for reading!

func diskImageDownloader(diskSpaceMB: Int = 100) -> ImageDownloader { let diskCapacity = diskSpaceMB * 1024 * 1024 let diskCache = NSURLCache(memoryCapacity: 0, diskCapacity: diskCapacity, diskPath: "alamofireimage_disk_cache") let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.URLCache = diskCache let downloader = ImageDownloader(configuration: configuration) UIImageView.af_sharedImageDownloader = downloader return downloader } func getProfileImage(atURL url: String, onComplete: SRServiceResponse<UIImage> -> Void) { guard let imageURL = NSURL(string: url) else { // TODO: Fail here return } let request = NSURLRequest(URL: imageURL) let imageDownloader = self.diskImageDownloader() imageDownloader.downloadImage(URLRequest: request) { (response) in switch response.result { case .Success(let image): // Do something case .Failure(let error): // Do something } } }

推荐答案

要实现目标,请使 NSURLCache 用作磁盘缓存,实际上是自定义设置存储图像的到期日期:

To reach your goal make your NSURLCache which you use as diskCache really custom to set your own expiration date for the stored images:

class DiskCache: NSURLCache { private let constSecondsToKeepOnDisk = 30*24*60*60 // 30 days override func storeCachedResponse(cachedResponse: NSCachedURLResponse, forRequest request: NSURLRequest) { var customCachedResponse = cachedResponse // Set custom Cache-Control for Image-Response if let response = cachedResponse.response as? NSHTTPURLResponse, let contentType = response.allHeaderFields["Content-Type"] as? String, var newHeaders = response.allHeaderFields as? [String: String] where contentType.containsString("image") { newHeaders["Cache-Control"] = "public, max-age=\(constSecondsToKeepOnDisk)" if let url = response.URL, newResponse = NSHTTPURLResponse(URL: url, statusCode: response.statusCode, HTTPVersion: "HTTP/1.1", headerFields: newHeaders) { customCachedResponse = NSCachedURLResponse(response: newResponse, data: cachedResponse.data, userInfo: cachedResponse.userInfo, storagePolicy: cachedResponse.storagePolicy) } } super.storeCachedResponse(customCachedResponse, forRequest: request) } }

而不是每次重新使用共享实例时都创建新的 ImageDownloader 调用 downloadImage 方法: UIImageView.af_sharedImageDownloader.downloadImage(URLRequest:request)

Instead of creating a new ImageDownloader every time you could reuse the shared instance to call the downloadImage method: UIImageView.af_sharedImageDownloader.downloadImage(URLRequest: request)

更多推荐

AlamofireImage磁盘缓存不起作用

本文发布于:2023-11-26 02:59:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   磁盘   不起作用   AlamofireImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!