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. 分页功能实现之通过ajax实现表单内容刷新

    拿代码来说话 我们的需求就是点击翻页功能,实现表格内容局部刷新且能够翻到对应的页面上,不明白? 那么就看看下面的图,需要达到的效果如下所示: 现在要实现的功能就是把红线框起来的表单内容 在点击翻页的时 ...

  2. Winform窗体控件自适应大小

    自己写的winform窗体自适应大小代码,代码比较独立,很适合贴来贴去不会对原有程序造成影响,可以直接继承此类或者把代码复制到自己的代码里面直接使用 借鉴了网上的一些资料,最后采用重写WndProc方 ...

  3. 消息中间件的意义和应用场景 (activeMq)

    消息中间件一般两个功能,解耦和异步处理,分别举个例子吧 解耦合:比如我们做一个微博产品中的好友系统,就很需要使用消息中间件当我们添加一个关注的时候, 涉及以下几个子系统 推荐系统,需要根据你关注的人给 ...

  4. linux 中搜索命令的对比

    1.find find是最常用和最强大的查找命令.它能做到实时查找,精确查找,但速度慢. find的使用格式如下: #find [指定目录] [指定条件] [指定动作] 指定目录:是指所要搜索的目录和 ...

  5. [原]巧用RenderTexture

    郑重声明:转载请注明出处 U_探索 本文诞生于面试过程中这道题:NGUI如何制作3D角色的显示.(大概是这样)  呵呵 没事出去面试面试,考核考核自己也是一种不错的方式哦!不过现在u3d面试,貌似比以 ...

  6. 用Eclipse编写Android程序的代码提示功能

    用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示     Window->Preferences- ...

  7. phonegap入门–3 Android phonegap 自定义插件DEMO

    一.环境要求:      首先需要建立phonegap android 工程,请参考:http://www.cnblogs.com/zhujinguo/p/4369883.html 二.建立java类 ...

  8. nodejs服务器部署教程一

    第一篇教程紧紧让你输出一个hello world 环境介绍 服务器环境:ubuntu(16.04)64位 本地环境:windows10 64位 连接工具:mobaxterm ubuntu安装和基本配置 ...

  9. C#索引器理解

    C#索引器介绍举例 索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 ( ...

  10. Explaining Delegates in C# - Part 1 (Callback and Multicast delegates)

    I hear a lot of confusion around Delegates in C#, and today I am going to give it shot of explaining ...