在将图像上传到服务器之前,如何压缩图像库中的视频或动态拍摄?

时间:2016-03-21 08:25:53

标签: swift avfoundation

following this thread,我试图重新创建他用来压缩视频的功能,但我得到了这个结果:

此应用程序正在从后台线程修改autolayout引擎,这可能导致引擎损坏和奇怪的崩溃。这将在将来的版本中引发异常。

这是我的代码:

let video = info[UIImagePickerControllerMediaURL] as! NSURL!
let uploadURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("\(NSDate())").URLByAppendingPathComponent(".mov")

                compressVideo(video, outputURL: uploadURL, handler: { (handler) -> Void in
                    if handler.status == AVAssetExportSessionStatus.Completed
                    {
                        let data = NSData(contentsOfURL: uploadURL)

                        print("File size after compression: \(Double(data!.length / 1048576)) mb")

                        self.dismissViewControllerAnimated(true, completion: nil)


                    }

                    else if handler.status == AVAssetExportSessionStatus.Failed {
                        let alert = UIAlertView(title: "Uh oh", message: " There was a problem compressing the video maybe you can try again later. Error: \(handler.error!.localizedDescription)", delegate: nil, cancelButtonTitle: "Okay")
                        alert.show()
                    }
                })



    func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession) -> Void)
{
    let urlAsset = AVURLAsset(URL: inputURL, options: nil)

    let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality)

    exportSession!.outputURL = outputURL

    exportSession!.outputFileType = AVFileTypeQuickTimeMovie

    exportSession!.shouldOptimizeForNetworkUse = true

    exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in

        handler(session: exportSession!)
    }

}

0 个答案:

没有答案