我现在正在斯坦福大学iOS Swift赋值6中,其中一个必需的任务是使用URLCache在本地磁盘上缓存映像。在谷歌上搜索了几天之后,我仍然想不出如何使用它。如果有人能给我指点一个好的向导,那会很有帮助的!
在尝试理解正式文档之后,我的代码现在是这样的。官方文档中没有我可以参考的示例代码,这是没有帮助的:
let urlCache = URLCache.shared所需的任务是设置缓存并指定大小限制。我尝试初始化URLCache并在参数中传递大小。它可以工作,但是存储和获取缓存似乎不起作用。如果每次启动应用程序(或视图控制器)时都初始化URLCache,难道它不会忽略之前创建和存储的缓存吗?
我觉得这个代码没有问题吗?从缓存读取数据
if let cachedURLResponse = urlCache.cachedResponse(for: URLRequest(url: url)) {
if let fetchedImage = UIImage(data: cachedURLResponse.data) {
image = fetchedImage
}
}我无法将数据写到缓存中
urlCache.storeCachedResponse(CachedURLResponse(response: URLResponse(), data: imageData), for: URLRequest(url: url))如何正确初始化URLResponse?我查看了init方法,它还要求将url作为参数传入。发现这个奇怪的地方,因为url也在URLRequest()中。我做错了吗?
很有帮助的建议,非常感谢!
发布于 2018-03-15 14:04:38
您可以使用URLCache使用URLSession请求图像数据,然后使用它的完成处理程序中可用的数据和响应,例如:
import UIKit
class GalleryCollectionViewController: UICollectionViewController, UICollectionViewDragDelegate, UICollectionViewDropDelegate, UICollectionViewDelegateFlowLayout {
// MARK: - Model
var gallery: Gallery?
// MARK: - Properties
private var cache = URLCache.shared
private var session = URLSession(configuration: .default)
override func viewDidLoad() {
super.viewDidLoad()
cache = URLCache(memoryCapacity: 100, diskCapacity: 100, diskPath: nil) // replace capacities with your own values
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryCell", for: indexPath)
if let galleryCell = cell as? GalleryCollectionViewCell {
galleryCell.image = nil
galleryCell.imageView.isHidden = true
if let url = gallery?.images[indexPath.item].url {
let request = URLRequest(url: url.imageURL) // imageURL from Utilities.swift of Stanford iOS course
if let cachedResponse = cache.cachedResponse(for: request), let image = UIImage(data: cachedResponse.data) {
galleryCell.image = image
galleryCell.imageView.isHidden = false
} else {
DispatchQueue.global(qos: .userInitiated).async { [weak self, weak galleryCell] in
let task = self?.session.dataTask(with: request) { (urlData, urlResponse, urlError) in
DispatchQueue.main.async {
if urlError != nil { print("Data request failed with error \(urlError!)") }
if let data = urlData, let image = UIImage(data: data) {
if let response = urlResponse {
self?.cache.storeCachedResponse(CachedURLResponse(response: response, data: data), for: request)
}
galleryCell?.image = image
} else {
galleryCell?.image = UIImage(named: "placeholder")
}
galleryCell?.imageView.isHidden = false
}
}
task?.resume()
}
}
}
}
return cell
}
}https://stackoverflow.com/questions/49249622
复制相似问题