将文件从 temp url 复制到文档目录总是会抛出错误。 NSURLSessionDownloadTask
Copying file from temp url to documents directory always throws. NSURLSessionDownloadTask
我正在使用 NSURLSessionDownloadTask 下载文件,效果很好。当我将图像或视频文件放入临时目录时。但我需要将其移动到永久 URL 才能将其放入照片库。我使用 NSFileManager
的 copyItemAtURL:
没有任何成功。它会抛出任何原因吗?也许文件类型与文档目录不兼容?
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)
Error Copying Item from
Optional("/private/var/mobile/Containers/Data/Application/E8D9C365-15D2-40BD-B0B5-A000BEDA9F00/Library/Caches/com.apple.nsurlsessiond/Downloads/$(AppID)/CFNetworkDownload_CK3G3Z.tmp")
to
Optional("/var/mobile/Containers/Data/Application/E8D9C365-15D2-40BD-B0B5-A000BEDA9F00/Documents/shortMovie.mov")
您使用了错误的 NSURL 初始值设定项。使用路径时,您需要使用 fileURLWithPath 初始值设定项。
我正在使用 NSURLSessionDownloadTask 下载文件,效果很好。当我将图像或视频文件放入临时目录时。但我需要将其移动到永久 URL 才能将其放入照片库。我使用 NSFileManager
的 copyItemAtURL:
没有任何成功。它会抛出任何原因吗?也许文件类型与文档目录不兼容?
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)
Error Copying Item from Optional("/private/var/mobile/Containers/Data/Application/E8D9C365-15D2-40BD-B0B5-A000BEDA9F00/Library/Caches/com.apple.nsurlsessiond/Downloads/$(AppID)/CFNetworkDownload_CK3G3Z.tmp") to Optional("/var/mobile/Containers/Data/Application/E8D9C365-15D2-40BD-B0B5-A000BEDA9F00/Documents/shortMovie.mov")
您使用了错误的 NSURL 初始值设定项。使用路径时,您需要使用 fileURLWithPath 初始值设定项。