gpt4 book ai didi

swift - 将文件从临时 url 复制到文档目录总是会抛出错误。 NSURLSessionDownloadTask

转载 作者:行者123 更新时间:2023-11-28 16:19:24 25 4
gpt4 key购买 nike

我正在使用 NSURLSessionDownloadTask 下载文件,效果很好。当我将图像或视频文件放入临时目录时。但我需要将它移动到永久 URL 才能将其放入照片库。我正在使用 NSFileManagercopyItemAtURL: 但没有成功。它会抛出任何原因吗?也许文件类型与文档目录不兼容?

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 初始值设定项。

关于swift - 将文件从临时 url 复制到文档目录总是会抛出错误。 NSURLSessionDownloadTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com