的一個問題是,你要加載的視頻(這是相當大的)到內存在NSData
。相反,如果您可以在持久性存儲中傳輸文件和從文件傳輸文件,則會更好。您可以使用NSURLSession
下載任務而不是使用已棄用的NSURLConnection
方法,即sendSynchronousRequest
來完成此操作。
通過使用NSURLSession
下載任務,您避免了一次試圖在內存中保存大型視頻,而是直接將視頻流傳輸到持久性存儲。 (注意,不要使用NSURLSession
數據的任務,因爲這將有相同的內存佔用問題的方法已過時的NSURLConnection
sendSynchronousRequest
)
一旦NSURLSession
下載任務流的直接下載到永久存儲,然後你可以將文件移動到臨時文件,然後使用addResourceWithType
,再次提供文件URL而不是NSData
。
當我這樣做(和添加一些其他有用的錯誤檢查),它似乎很好地工作:
// make sure it's authorized
PHPhotoLibrary.requestAuthorization { authorizationStatus in
guard authorizationStatus == .Authorized else {
print("cannot proceed without permission")
return
}
self.downloadVideo()
}
其中:
func downloadVideo() {
let fileManager = NSFileManager.defaultManager()
// create request
let url = NSURL(string: "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_10mb.mp4")!
let task = NSURLSession.sharedSession().downloadTaskWithURL(url) { location, response, error in
// make sure there weren't any fundamental networking errors
guard location != nil && error == nil else {
print(error)
return
}
// make sure there weren't and web server errors
guard let httpResponse = response as? NSHTTPURLResponse where httpResponse.statusCode == 200 else {
print(response)
return
}
// move the file to temporary folder
let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory())
.URLByAppendingPathComponent(url.lastPathComponent!)
do {
try fileManager.moveItemAtURL(location!, toURL: fileURL)
} catch {
print(error)
return
}
// now save it in our photo library
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetCreationRequest.creationRequestForAsset().addResourceWithType(.Video, fileURL: fileURL, options: nil)
}, completionHandler: { success, error in
defer {
do {
try fileManager.removeItemAtURL(fileURL)
} catch let removeError {
print(removeError)
}
}
guard success && error == nil else {
print(error)
return
}
print("SUCCESS")
})
}
task.resume()
}
注意,因爲NSURLSession
是關於如何確保更嚴格您不需要執行不安全的請求,您可能需要更新info.plist
(右鍵單擊它並選擇「Open As」 - 「Source Code」)並將其添加到文件中:
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>sample-videos.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
但是,當我做了所有這些,視頻被下載併成功添加到我的照片庫。請注意,我在此刪除了所有同步請求(NSURLSession
是異步的,因爲是performChanges
),因爲您幾乎不想執行同步請求(並且絕對不會在主隊列中)。
來源
2016-08-03 04:33:04
Rob
而不是使用'try!',使用''''''''''''''''''模式並打印出結果錯誤'對象。它可能會給你一個更有意義的錯誤信息。 – Rob
當我嘗試時出現類似的錯誤:錯誤域= NSCocoaErrorDomain代碼= -1「(null)」 – user2658229
我是iOS編程新手,所以我仍在學習,因爲我要去。我很抱歉,您所指的是localizedDescription和userInfo是什麼?是的,該應用程序已請求並訪問照片庫。 – user2658229