zoukankan      html  css  js  c++  java
  • iOS

     

    UIVideoEditorController类包含了由系统提供的界面,使用户可以交互式的剪切视频。UIVideoEditorController对象处理用户的交互并且提供把编辑后的视频的文件系统路径提供给UIVideoEditorControllerDelegate对象.

    UIVideoEditorController只支持能够支持视频编辑的设备. 
    我们设置好它的delegate及videoPath属性,并将其展示出来。(经过videoQuality属性,也可以通过这个类将视频重新编码成质量较低的格式)

    @property(nullable, nonatomic,assign) id <UINavigationControllerDelegate, UIVideoEditorControllerDelegate> delegate; @property(nonatomic, copy) NSString *videoPath; // 视频路径

    @property(nonatomic) NSTimeInterval videoMaximumDuration; // default value is 10 minutes. set to 0 to specify no maximum duration. @property(nonatomic) UIImagePickerControllerQualityType videoQuality; // default value is UIImagePickerControllerQualityTypeMedium

    UIVideoEditorController和UIImagePickerController的主要区别是前者能提供视频的编辑,后者主要用于录像或者视频的选择. 
    UIVideoEditorController视频编辑器所用的delegate回调与UIImagePickerController类相似,这些回调方法分别来处理成功、失败、取消这三种情况:

    videoEditorController:didSaveEditedVideoToPath:

    videoEditorController: didFailWithError:

    videoEditorControllerDidCancel:

    #import <AVFoundation/AVFoundation.h>

    #import "ViewController.h"

    @interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIVideoEditorControllerDelegate>

    @end

    @implementation ViewController

    - (void)viewDidLoad {

         [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.

    }

    - (void)didReceiveMemoryWarning {

          [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.

    }

    - (IBAction)click:(UIButton *)sender {

          UIImagePickerController *myImagePickerController = [[UIImagePickerController alloc] init];

          myImagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

          myImagePickerController.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:myImagePickerController.sourceType];

          myImagePickerController.delegate = self; myImagePickerController.editing = NO;

          [self presentViewController:myImagePickerController animated:YES completion:nil];

    }

    #pragma mark - UIImagePickerControllerDelegate

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

          [picker dismissViewControllerAnimated:YES completion:^{

                  NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

                  if([mediaType isEqualToString:@"public.movie"]) {

                         NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

                         UIVideoEditorController *editVC; // 检查这个视频资源能不能被修改

                        if ([UIVideoEditorController canEditVideoAtPath:videoURL.path]) {

                                editVC = [[UIVideoEditorController alloc] init];

                                editVC.videoPath = videoURL.path;

                                editVC.delegate = self;

                        }

                       [self presentViewController:editVC animated:YES completion:nil];

                    }

           }];

    }

    //编辑成功后的Video被保存在沙盒的临时目录中

    - (void)videoEditorController:(UIVideoEditorController *)editor didSaveEditedVideoToPath:(NSString *)editedVideoPath {

           NSLog(@"+++++++++++++++%@",editedVideoPath);

    }
    // 编辑失败后调用的方法

    - (void)videoEditorController:(UIVideoEditorController *)editor didFailWithError:(NSError *)error {

           NSLog(@"%@",error.description);

    }

    //编辑取消后调用的方法

    - (void)videoEditorControllerDidCancel:(UIVideoEditorController *)editor {}

    @end

    这是swift版本:

    class ViewController: UIViewController, UIVideoEditorControllerDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate {

         var editVideoViewController:UIVideoEditorController!

         @IBAction func editVideoTapped(sender: AnyObject) {

              let imagePicker = UIImagePickerController()

              imagePicker.delegate = self

              imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

              let types = UIImagePickerController.availableMediaTypesForSourceType(.Camera)!

              imagePicker.mediaTypes = [types[1]]//kUTTypeMovie

              self.presentViewController(imagePicker, animated: true, completion: nil)

    }

    //MARK: - UIImagePickerControllerDelegate

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

            picker.dismissViewControllerAnimated(true) { () -> Void in

                 let mediaType:String = info[UIImagePickerControllerMediaType] as! String

                 if mediaType == "public.movie" {

                      let url: NSURL = info[UIImagePickerControllerMediaURL] as! NSURL

                      let editVideoViewController = UIVideoEditorController()

                     //设置delegate

                    editVideoViewController.delegate = self

                    //设置要编辑的视频地址

                    editVideoViewController.videoPath = videoPath!

                    self.presentViewController(editVideoViewController, animated: true, completion: nil)

                  }

              }

    }

    //MARK: - UIVideoEditorControllerDelegate

    //编辑成功后的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: {})

           }

    }

    这是效果图 :

    这里写图片描述


    开源链接分享

    使用UIVideoEditController进行视频编辑是基于苹果封装好的编辑类,要是我们实现自定义视频截取,就要自己写了,在这里分享一个视频编辑并裁剪的一个第三方,地址是 https://github.com/itsmeichigo/ICGVideoTrimmer.git,使用效果不错!

  • 相关阅读:
    点击文本变成输入框
    html代码片段
    node 开启Gzip压缩
    npm 安装与卸载
    console.dir()-----js中console.log()和console.dir()的区别
    javaScript学习笔记之-------this
    javaScript学习笔记之-------闭包
    从零搭建vue项目---VUE从无到有
    require.js扫盲版
    cross-env 解决跨平台设置的NODE_ENV的问题
  • 原文地址:https://www.cnblogs.com/gongyuhonglou/p/9031019.html
Copyright © 2011-2022 走看看