我的collectionView控制器中有一些加载问题。当我上传视频时,它将制作该视频的屏幕截图,然后将其缓存,因此不会再次下载。当滚动滚动上载新视频时,缓存可以完美地工作,它将冻结整个滚动,因此可以下载新的视频屏幕截图。我有做“事后技巧”的波纹管功能。我还试图结合第二个功能在下载完成后触发此功能,但是我不知道该如何使它工作。
func previewImageFromVideo(url: NSURL) -> UIImage? {
let url = url as URL
let request = URLRequest(url: url)
let cache = URLCache.shared
if
let cachedResponse = cache.cachedResponse(for: request),
let image = UIImage(data: cachedResponse.data)
{
return image
}
let asset = AVAsset(url: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
imageGenerator.maximumSize = CGSize(width: 500, height: 500)
var time = asset.duration
time.value = min(time.value,2)
var image: UIImage?
do {
let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil)
image = UIImage(cgImage: imageRef)
} catch { }
if
let image = image,
let pngData = UIImagePNGRepresentation(image)
{
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!
let cachedResponse = CachedURLResponse(response: response, data: pngData)
cache.storeCachedResponse(cachedResponse, for: request)
}
return image
}
这是开始下载文件和强制功能,它将告诉我何时完成:
//SESSION URL - DOWNLOAD WITH PROGRESS
func beginDownloadingFile(urlString: String) {
print("Attempting to download file")
let configuration = URLSessionConfiguration.default
let operationQueue = OperationQueue()
let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)
guard let url = URL(string: urlString) else { return }
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
//DOWNLOAD WITH PROGRESS - BASIC APPROACH
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Finished downloading file")
}