ios - 快速将视频 NSData 写入图库

标签 ios objective-c iphone swift xcode

我正在 swift 上编写一个 iOS 应用程序,它从 URL 下载视频并将其写入磁盘。我正在获取数据,但到目前为止未能成功写入磁盘。下面是代码:

let yourURL = NSURL(string: "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_10mb.mp4")
    //Create a URL request
    let urlRequest = NSURLRequest(URL: yourURL!)
    //get the data
    var theData = NSData();
    do{
        theData = try NSURLConnection.sendSynchronousRequest(urlRequest, returningResponse: nil)
    }
    catch let err as NSError
    {

    }

    try! PHPhotoLibrary.sharedPhotoLibrary().performChangesAndWait({ ()-> Void in
        if #available(iOS 9.0, *) {
            PHAssetCreationRequest.creationRequestForAsset().addResourceWithType(PHAssetResourceType.Video, data: theData, options: nil)

            print("SUCESS");
        } else {

        };

    });

我收到以下错误,任何见解表示赞赏:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=-1 "(null)": file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.1/src/swift/stdlib/public/core/ErrorType.swift, line 54

最佳答案

一个问题是您试图将视频(可能非常大)加载到 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(右键单击它并选择“打开为”-“源代码”)并将其添加到文件中:

<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 也是异步的),因为您几乎不想执行同步请求(当然也不想在主队列上执行) ).

关于ios - 快速将视频 NSData 写入图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38732839/

相关文章:

ios - 为什么 Main.storyboard 不显示?

ios - Apple 方式的 UITableView 事件指示器

ios - 在 Xcode 中显示所有弃用的警告

ios - 如何使用 slider 更改 UICollectionViewCell 的框架?

ios - 更新后保存的照片不会出现在设备上

iphone - 由于libdispatch-manager而崩溃

ios - 无法将类型 '__NSCFArray' 的值转换为 'NSDictionary' + 在 map View 中显示注释

iOS 7 - 静默推送通知

iOS Realm 更新 block 中的对象

iphone - CALayer 的 display/drawRect 方法中到底应该发生什么?