following this thread, i tried to recreate the function he used to compress a video, but I got this as a result:
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
here's my code:
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!)
}
}