我正在使用运行良好的 NSURLSessionDownloadTask 下载文件。当我将图像或视频文件放入临时目录时。但我需要将其移动到永久 URL 才能将其放入照片库。我正在使用NSFileManager
'scopyItemAtURL:
没有任何成功。有什么理由会抛出吗?也许文件类型与文档目录不兼容?
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)")
}
}
}
}
这是打印语句。为了安全起见,我将文档目录中的应用程序包 ID 替换为$(AppId)
从 Optional 复制项目时出错(“/private/var/mobile/Containers/Data/Application/E8D9C365-15D2-40BD-B0B5-A000BEDA9F00/Library/Caches/com.apple.nsurlsessiond/Downloads/$(AppID)/CFNetworkDownload_CK3G3Z.tmp ") 到可选的("/var/mobile/Containers/Data/Application/E8D9C365-15D2-40BD-B0B5-A000BEDA9F00/Documents/shortMovie.mov")