首页 > 代码库 > UIVideoEditorController的使用

UIVideoEditorController的使用

UIVideoEditorController是一个视频编辑器,通过系统提供的UI界面来剪切视频或者降低视频的画质.UIVideoEditorController对象处理用户的交互并且提供把编辑后的视频的文件系统路径提供给UIVideoEditorControllerDelegate对象.

UIVideoEditorController支持能够支持视频编辑的设备.

UIVideoEditorControllerUIImagePickerController的主要区别是前者能提供视频的编辑,后者主要用于录像或者视频的选择.

实现代码:

//遵守协议
class ViewController: UIViewController, UIVideoEditorControllerDelegate, UINavigationControllerDelegate {
    var editVideoViewController:UIVideoEditorController!
    
    @IBAction func editVideoTapped(sender: AnyObject) {
        //取到Video的资源
        let videoPath = NSBundle.mainBundle().pathForResource("show", ofType: "mov");
        
        //创建之前要确定是否能够打开视频文件,如果能打开才创建UIVideoEditController
        if UIVideoEditorController.canEditVideoAtPath(videoPath!) {
            //创建UIVideoEditController
            editVideoViewController = UIVideoEditorController()
            //设置delegate
            editVideoViewController.delegate = self
            //设置要编辑的视频地址
            editVideoViewController.videoPath = videoPath!
            //设置视频最长时间
            editVideoViewController.videoMaximumDuration = 30
            //设置视频的画质(如果TypeHigh,则画质不变)
            editVideoViewController.videoQuality = .TypeHigh
            //present UIVideoEditController
            presentViewController(editVideoViewController, animated: true, completion: {})
        }
    }

    //UIVideoEditorController自己不会主动小时,所以delegate的回调方法的一个主要作用是消除UIVideoEditorController,
    //编辑成功后的Video被保存在沙盒的临时目录中
    func videoEditorController(editor: UIVideoEditorController, didSaveEditedVideoToPath editedVideoPath: String) {
        print("editedVideopath = \(editedVideoPath)")
        dismissViewControllerAnimated(true, completion: {})
    }
    //编辑失败后调用的方法
    func videoEditorController(editor: UIVideoEditorController, didFailWithError error: NSError) {
        print("error=\(error.description)")
        dismissViewControllerAnimated(true, completion: {})
    }
    //编辑取消后调用的方法
    func videoEditorControllerDidCancel(editor: UIVideoEditorController) {
        dismissViewControllerAnimated(true, completion: {})
    }
}

效果图:

技术分享




UIVideoEditorController的使用