始终将文件从temp url复制到文档目录。 NSURLSessionDownloadTask

时间:2016-07-29 17:06:18

标签: swift nsurlsession nsfilemanager phphotolibrary

我正在使用NSURLSessionDownloadTask下载文件,该文件运行良好。当我将我的图像或视频文件放入临时目录时。但我需要将其移动到永久URL,以便将其放入照片库。我正在使用NSFileManager的{​​{1}}但没有任何成功。它会抛出的任何理由?也许文件类型与文档目录不兼容?

copyItemAtURL:

这是print语句。为了安全起见,我正在用let directory : String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) { if let fileName = self.file.zOrigFileName { let destinationPath = self.directory.stringByAppendingString("/\(fileName)") if let destinationURL = NSURL(string: destinationPath) { let fileManager = NSFileManager.defaultManager() //IF file with same name exists delete it before copying new file if fileAlreadyExistsAtURL(destinationURL) { do { try fileManager.removeItemAtURL(destinationURL) } catch { print("Error Removing Item At \(destinationURL.path)") } } do { try fileManager.copyItemAtURL(location, toURL: destinationURL) self.saveURLToPhotosLibrary(destinationURL) } catch { //This is line always printing. my try statement always throwing. print("Error Copying Item from \(location.path) to \(destinationURL.path)") } } } }

替换文档目录中的app bundle id
  

从中复制项目时出错   可选( “/私营/无功/移动/集装箱/数据/应用/ E8D9C365-15D2-40BD-B0B5-​​A000BEDA9F00 /图书馆/缓存/ com.apple.nsurlsessiond /下载/ $(的AppID)/CFNetworkDownload_CK3G3Z.tmp”)   至   可选( “/变种/移动/容器/数据/应用/ E8D9C365-15D2-40BD-B0B5-​​A000BEDA9F00 /文档/ shortMovie.mov”)

1 个答案:

答案 0 :(得分:1)

您使用的是错误的NSURL初始值设定项。在使用机智路径时,您需要使用fileURLWithPath初始化程序。

相关问题