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. wordpress主题升级之后返回到原来版本主题的方法

    wordpress后台经常可以看到主题提示升级,但是发现升级之后样式,颜色等都变了,不是以前的样子了,这时候如果想要返回到以前版本,前提,必须以前版本有备份. 在wordpress里面找到主题===添 ...

  2. flume配置文件

    读文件log传入kafka中 agent.sources = s1 agent.channels = c1 agent.sinks = k1 agent.sources.s1.type=exec ag ...

  3. git链接github仓库

    配置Git 我们先在电脑硬盘里找一块地方存放本地仓库,比如我们把本地仓库建立在C:\MyRepository\1ke_test文件夹下 进入1ke_test文件夹 鼠标右键操作如下步骤: 1)在本地仓 ...

  4. 九度 1552 座位问题(递推DP)

    题目描述: 计算机学院的男生和女生共n个人要坐成一排玩游戏,因为计算机的女生都非常害羞,男生又很主动,所以活动的组织者要求在任何时候,一个女生的左边或者右边至少有一个女生,即每个女生均不会只与男生相邻 ...

  5. vuejs时间格式化

    date.js export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ...

  6. ios开发之--多个按钮单选效果

    开发项目时,有很多场景需要用到按钮单选效果,例如充值页面,选择标签页面等,具体实现代码如下: 1,创建 -(UIView *)headerView { CGFloat width = (Kscreen ...

  7. 在eclipse中查看android源代码

    自己写了一个类MainAcvitivity extends Activity, 按F12(我把转到定义改成了F12的快捷键),转到Activity的定义,弹出下面这样的界面 就是说没有找到androi ...

  8. backbone学习笔记:模型(Model)(1)基础知识

    backbone为复杂Javascript应用程序提供MVC(Model View Controller)框架,框架里最基本的是Model(模型),它用来处理数据,对数据进行验证,完成后台数据与前台数 ...

  9. javascript 作用域、作用域链理解

    JavaScript作用域就是变量和函数的可访问范围. 1.变量作用域 在JavaScript中,变量作用域分为全局作用域和局部作用域. 全局作用域 任何地方都可以定义拥有全局作用域的变量 1.没有用 ...

  10. 【Android】水平居中 垂直居中 中心居中

    android:layout_centerInParent 将该组件放置于水平方向中央及垂直中央的位置 android:layout_centerHorizontal 将该组件放置于水平方向中央的位置 ...