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. Winform窗体控件自适应大小

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

  2. NAS 创建大文件

      不是很懂,但是管用.先记录下来. http://www.111cn.net/sys/linux/55537.htm

  3. MySQL------报错Access denied for user 'root'@'localhost' (using password:NO)解决方法

    报错:Access denied for user 'root'@'localhost' (using password:NO) 原因:没有给用户“root'@'localhost”赋予数据库权限 解 ...

  4. Linux+Redis实战教程_day01_Linux系统上安装tomcat

    Linux系统上安装tomcat 安装tomcat 上传tomcat的安装文件 Alt+p 拖拽上传 创建tomcat的安装路径 mkdir -p /usr/local/tomcat 解压tomcat ...

  5. python的输出问题

    我们知道python提供了一个shell来供初学者学习,在shell里是输入一句执行一句,例如:

  6. iOS NSError

    写在前面 在iOS开发中,NSError的使用非常常见,使用也比较简单,也正因为简单,所以对这一部分知识不甚注重.但是近期在做app底层网络封装时发现了一些问题.我使用的网络框架是AFNetworki ...

  7. iOS UTI(统一类型标识)

    同一类型标识符(Uniform Type Identifier,UTI)代表IOS信息共享的中心组件.可以把它看成下一代的MIME类型.UTI是标识资源类型(比如图像和文本)的字符串,他们制定哪些类型 ...

  8. Ansible Playbook 使用变量

    如何在 Playbook 中定义并使用变量: vars: - user: "test" # 定义变量 tasks: - name: create user user: name=& ...

  9. Ansible常用模块使用

    Ansible官方提供了非常多的模块,还有若干第三方模块,我们也可以自己编写模块. Ansible对远程服务器的操作实际是通过模块完成的,先将模块拷贝到远程服务器,完成操作后,然后在远程服务器上删除该 ...

  10. C语言中一个字符数组里面的所有元素变成一个字符串

    #include <string.h> int main() // 这里为了方便直接用main函数 {     char array[] = { 'h', 'e', 'l', 'l', ' ...