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进行视频编辑的更多相关文章

  1. ios开发中如何调用苹果自带地图导航

    前段时间一直在赶项目,在外包公司工作就是命苦,天天加班不说,工作都是和工期合同挂钩的,稍微逾期就有可能被扣奖金,不谈这些伤脑筋的事情了,让我们说说iOS开发中如何调用苹果手机自带的地图. 学习如逆水行 ...

  2. iOS苹果自带UIMenuController

    一.UIMenuController认识 1.默认情况下,UITextView / UITextFiled / UIWebView 都有苹果自带的有UIMenuController功能 2.UITex ...

  3. iOS之苹果和百度地图的使用

    iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).其中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...

  4. 【iOS】苹果,百度Map定位使用与总结

    iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).当中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...

  5. 操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统)

    ylbtech-操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统) iOS是由苹果公司开发的移动操作系统.苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计 ...

  6. ios(苹果公司的移动操作系统)

    iOS是由苹果公司开发的移动操作系统. 苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计给iPhone使用的, 后来陆续套用到iPod touch.iPad以及Appl ...

  7. IOS开发苹果官方Sample Code及下载地址

    IOS开发苹果官方Sample Code及下载地址 在线浏览地址:https://developer.apple.com/library/ios/navigation/#section=Resourc ...

  8. iOS开发苹果内购的介绍与实现

    1.iOS开发苹果内购的介绍 1.1 介绍 苹果规定,凡是虚拟的物品(例如:QQ音乐的乐币)进行交易时,都必须走苹果的内购通道,苹果要收取大约30%的抽成,所以不允许接入第三方的支付方式(微信.支付宝 ...

  9. iOS面试题整理带答案

    iOS面试题整理带答案       找工作,面试是避免不了的! 而技术开发面试,问一些技术相关的问题是必须的,最新的技术可能人人都趋之若鹜,但有些原理和基础的也希望都有了解. 这里整理了一些iOS相关 ...

随机推荐

  1. Linux下的ssh远程访问

    准备工作:首先需要在windows系统中安装虚拟机,并在虚拟机中安装好linux操作系统,这里安装的是vmware player虚拟机和ubuntu版本的操作系统.关于该部分的安装在作者的其他经验中有 ...

  2. tomcat 重启报错unregister mbean error javax.management.InstanceNotFoundException

    JAVA_OPTS='-Ddruid.registerToSysProperty=true' 多个应用部署到tomcat下需要在/bin/catalina.sh下添加上面这句话 添加位置在‘cygwi ...

  3. python中的字符串常量,是否支持通过下标的方式赋值

    说明: 今天在看python,通过下标获取字符串常量的字符,在想是否可以通过下标的方式赋值. 操作: 1.对字符串下标赋值 >>> text='python' >>> ...

  4. linux(redhat)环境下,如何解压rar文件?

    需求描述: 今天一个同事发过来一个压缩文件,让传送到linux服务器上,传上去之后,发现是rar格式的 之前没有解压过,就找了一些解压方法,在此记录下. 操作过程: 1.下载rar在linux环境下的 ...

  5. ubuntu alsa2

    ALSA是Advanced Linux Sound Architecture简称.它包含一组kernel 驱动,一个应用编程接口(API)库以及一组工具函数.本文中,我们会向读者展示ALSA项目和组成 ...

  6. Android跑指定包Monkey脚本

    Android跑指定包Monkey脚本 adb shell monkey –p com.android.mms --throttle 1000 -v -v -v -s 1 --ignore-secur ...

  7. C#客户端嵌入Chrome浏览器的实现

    https://blog.csdn.net/lanwilliam/article/details/79639823 客户端软件,也就是传统的Winform软件,在很多时候是很好用的.因为在做一些打印. ...

  8. NSIS安装vcredist_64.exe

    ; ExecWait ‘vcredist_x86.exe’ # 一般的安装ExecWait ‘”vcredist_x86.exe” /q’ # silent install 静默安装; ExecWai ...

  9. Oracle的闪回技术--闪回已删除的表

    注意闪回技术只能保护非系统表决空间中的表,而且表空间必须本地管理, 外键将不可以被恢复, 索引和约束的名字将会被命名为以BIN开头,由系统生成的名字 查看是否开启闪回: SQL> show pa ...

  10. 高并发应对:淘宝CDN缓存服务器部署探秘

    转自:http://server.chinabyte.com/6/12663506.shtml “好,时间到,开抢!”坐在电脑前早已等待多时的宋兰(化名)一看时间已到2011年11月11日零时,便迫不 ...