iOS - 使用苹果自带的UIVideoEditController进行视频编辑

Posted 公羽寒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS - 使用苹果自带的UIVideoEditController进行视频编辑相关的知识,希望对你有一定的参考价值。

 

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,使用效果不错!




以上是关于iOS - 使用苹果自带的UIVideoEditController进行视频编辑的主要内容,如果未能解决你的问题,请参考以下文章

ios之苹果和百度地图的使用

ios 苹果和百度地图的相关使用

我的苹果手机自带java系统我想下载IOS系统怎么下

苹果手机有好用的录屏软件吗?

手机自带浏览器下载不到东西怎么办?

苹果升级到iOS10后使用高德地图导航一直提示GPS信号弱,这样怎么解决?