我现在在斯坦福iOS Swift Assignment 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?我查看了in it方法,它还要求url作为参数传入。发现这个奇怪,因为url也在URLRequest()中。我做错了吗?
非常感谢您的忠告!

最佳答案

可以使用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
 }
}

10-08 08:05