操作无法完成。 (cocoa错误-1。) – PHPhotoLibrary

嗨,我想保存一个下载的MP4文件到我的画廊。

使用downloadTaskurl

 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL){ } 

我得到的url

 file:///private/var/mobile/Containers/Data/Application/8D761DFE-C050-426D-B91C-61DE441FB496/Library/Caches/com.apple.nsurlsessiond/Downloads/com.te.dn/CFNetworkDownload_KFSYSW.tmp 

我正在创build一个path,

 let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) let documentDirectoryPath:String = path[0] let fileManager = FileManager() let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file1.mp4")) if fileManager.fileExists(atPath: destinationURLForFile.path){ } else{ do { try fileManager.moveItem(at: location, to: destinationURLForFile) // show file }catch{ print("An error occurred while moving file to destination url") } } 

我可以成功地移动文件(移动时没有得到任何错误)。

然后我试图保存文件获取错误

 let url = URL(fileURLWithPath: destinationURLForFile.path) 

url的值是:

 file:///var/mobile/Containers/Data/Application/8DF0C849-56A1-42B3-A081-0C992D18F973/Documents/file1.mp4 PHPhotoLibrary.shared().performChanges({ PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url) }) { saved, error in if saved { let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) } else { print(error?.localizedDescription ?? "f") } } 

o / p:错误: – The operation couldn't be completed. (Cocoa error -1.) The operation couldn't be completed. (Cocoa error -1.)