使用UIImagePickerController 进行录制

  1. #import "ViewController.h"
  2. #import <MobileCoreServices/MobileCoreServices.h>
  3. #import <QuartzCore/QuartzCore.h>
  4.  
  5. @interface ViewController ()
  6. <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
  7.  
  8. - (IBAction)videoRecod:(id)sender;
  9.  
  10. @end
  11.  
  12. @implementation ViewController
  13.  
  14. - (void)viewDidLoad
  15. {
  16. [super viewDidLoad];
  17. // Do any additional setup after loading the view, typically from a nib.
  18. }
  19.  
  20. - (void)didReceiveMemoryWarning
  21. {
  22. [super didReceiveMemoryWarning];
  23. // Dispose of any resources that can be recreated.
  24. }
  25.  
  26. - (IBAction)videoRecod:(id)sender {
  27.  
  28. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  29.  
  30. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  31. imagePickerController.delegate = self;
  32. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  33.  
  34. imagePickerController.mediaTypes = [[NSArray alloc]
  35. initWithObjects:(NSString *)kUTTypeMovie, nil];
  36.  
  37. //录制质量设定
  38. imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
  39.  
  40. //仅仅同意最多录制30秒时间
  41. imagePickerController.videoMaximumDuration = 30.0f;
  42.  
  43. [self presentViewController:imagePickerController animated:YES completion:nil];
  44.  
  45. } else {
  46. NSLog(@"摄像头不可用。
  47.  
  48. ");
  49. }
  50. }
  51.  
  52. - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
  53. [self dismissViewControllerAnimated:YES completion:nil];
  54. }
  55.  
  56. - (void) imagePickerController: (UIImagePickerController *) picker
  57. didFinishPickingMediaWithInfo: (NSDictionary *) info {
  58.  
  59. NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
  60. NSString *tempFilePath = [url path];
  61.  
  62. if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath) ) {
  63. //video:didFinishSavingWithError:contextInfo: 必须保存成这类回调
  64. UISaveVideoAtPathToSavedPhotosAlbum( tempFilePath,
  65. self,
  66. @selector(video:didFinishSavingWithError:contextInfo:),
  67. (__bridge void *)(tempFilePath));
  68. }
  69.  
  70. [self dismissViewControllerAnimated:YES completion:nil];
  71.  
  72. }
  73.  
  74. - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(NSString *)contextInfo {
  75. NSString *title; NSString *message;
  76. if (!error) {
  77. title = @"视频保存";
  78. message = @"视频已经保存到设备的相机胶卷中";
  79. } else {
  80. title = @"视频失败";
  81. message = [error description];
  82. }
  83. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
  84. message:message
  85. delegate:nil
  86. cancelButtonTitle:@"OK"
  87. otherButtonTitles:nil];
  88. [alert show];
  89. }
  90.  
  91. - (void)navigationController:(UINavigationController *)navigationController
  92. willShowViewController:(UIViewController *)viewController
  93. animated:(BOOL)animated
  94. {
  95. NSLog(@"选择器将要显示。");
  96. }
  97.  
  98. - (void)navigationController:(UINavigationController *)navigationController
  99. didShowViewController:(UIViewController *)viewController
  100. animated:(BOOL)animated
  101. {
  102. NSLog(@"选择器显示结束。
  103.  
  104. ");
  105. }

使用AVFoundation

AVCaptureSession,捕获会话,是为了实现从摄像头和麦克风捕获数据,须要使用AVCaptureSession对象协调输入输出数据

AVCaptureDevice,捕获设备,代表输入一个设备,比如摄像头和麦克风

AVCaptureDeviceInput,捕获会话的一个输入数据源

AVCaptureOutput,捕获会话的一个输出目标,比如输出的视频文件和静态图片

AVCaptureMovieFileOutput,是AVCaptureOutput的子类,通过它能够将捕获的数据输出到QuickTime视频文件里(MOV)

AVCaptureVideoPreviewLayer,是CALayer子类,能够使用它来显示录制的视频

AVCaptureConnection,捕获连接,在一个捕获会话中输入和输出之间的连接

  1. #import "ViewController.h"
  2.  
  3. #import <AVFoundation/AVFoundation.h>
  4. #import <AssetsLibrary/AssetsLibrary.h>
  5.  
  6. @interface ViewController ()
  7. <AVCaptureFileOutputRecordingDelegate>
  8. {
  9. BOOL isRecording;
  10. }
  11.  
  12. @property (weak, nonatomic) IBOutlet UILabel *label;
  13. @property (weak, nonatomic) IBOutlet UIButton *button;
  14.  
  15. @property (strong, nonatomic) AVCaptureSession *session;
  16. @property (strong, nonatomic) AVCaptureMovieFileOutput *output;
  17.  
  18. - (IBAction)recordPressed:(id)sender;
  19.  
  20. @end
  21.  
  22. @implementation ViewController
  23.  
  24. - (void)viewDidLoad
  25. {
  26. [super viewDidLoad];
  27.  
  28. self.session = [[AVCaptureSession alloc] init];
  29. self.session.sessionPreset = AVCaptureSessionPresetMedium;
  30.  
  31. AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  32.  
  33. NSError *error = nil;
  34. AVCaptureDeviceInput *camera = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&error];
  35.  
  36. AVCaptureDevice *micDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  37. AVCaptureDeviceInput *mic = [AVCaptureDeviceInput deviceInputWithDevice:micDevice error:&error];
  38.  
  39. if (error || !camera || !mic) {
  40. NSLog(@"Input Error");
  41. } else {
  42. //增加捕获音频视频
  43. [self.session addInput:camera];
  44. [self.session addInput:mic];
  45. }
  46.  
  47. self.output = [[AVCaptureMovieFileOutput alloc] init];
  48.  
  49. if ([self.session canAddOutput:self.output]) {
  50. [self.session addOutput:self.output];//输出
  51. }
  52.  
  53. AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
  54. previewLayer.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
  55. [self.view.layer insertSublayer:previewLayer atIndex:0];
  56.  
  57. [self.session startRunning];
  58. isRecording = NO;
  59. self.label.text = @"";
  60.  
  61. }
  62.  
  63. - (void)didReceiveMemoryWarning
  64. {
  65. [super didReceiveMemoryWarning];
  66. }
  67.  
  68. - (void)viewWillAppear:(BOOL)animated
  69. {
  70. [super viewWillAppear:animated];
  71. if (![self.session isRunning])
  72. {
  73. [self.session startRunning];
  74. }
  75. }
  76.  
  77. - (void)viewWillDisappear:(BOOL)animated
  78. {
  79. [super viewWillDisappear:animated];
  80. if ([self.session isRunning])
  81. {
  82. [self.session stopRunning];
  83. }
  84. }
  85.  
  86. - (IBAction)recordPressed:(id)sender {
  87. if (!isRecording)
  88. {
  89. [self.button setTitle:@"停止" forState:UIControlStateNormal];
  90. self.label.text = @"录制中...";
  91. isRecording = YES;
  92. NSURL *fileURL = [self fileURL];
  93. [self.output startRecordingToOutputFileURL:fileURL recordingDelegate:self];
  94. }
  95. else
  96. {
  97. [self.button setTitle:@"录制" forState:UIControlStateNormal];
  98. self.label.text = @"停止";
  99. [self.output stopRecording];
  100. isRecording = NO;
  101. }
  102. }
  103.  
  104. - (NSURL *) fileURL
  105. {
  106. NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"movie.mov"];
  107. NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
  108.  
  109. NSFileManager *manager = [[NSFileManager alloc] init];
  110. if ([manager fileExistsAtPath:outputPath])
  111. {
  112. [manager removeItemAtPath:outputPath error:nil];
  113. }
  114.  
  115. return outputURL;
  116. }
  117.  
  118. #pragma mark-- AVCaptureFileOutputRecordingDelegate托付协议实现方法
  119.  
  120. - (void)captureOutput:(AVCaptureFileOutput *)captureOutput
  121. didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
  122. fromConnections:(NSArray *)connections error:(NSError *)error
  123. {
  124.  
  125. if (error == nil) {
  126. ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  127.  
  128. [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
  129. completionBlock:^(NSURL *assetURL, NSError *error)
  130. {
  131. if (error)
  132. {
  133. NSLog(@"写入错误。") ;
  134. }
  135.  
  136. }];
  137. }
  138.  
  139. }

使用UIVideoEditorController

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4. <UIVideoEditorControllerDelegate,UINavigationControllerDelegate>
  5.  
  6. - (IBAction)editButtonPress:(id)sender;
  7.  
  8. @end
  9.  
  10. @implementation ViewController
  11.  
  12. - (void)viewDidLoad
  13. {
  14. [super viewDidLoad];
  15. // Do any additional setup after loading the view, typically from a nib.
  16. }
  17.  
  18. - (void)didReceiveMemoryWarning
  19. {
  20. [super didReceiveMemoryWarning];
  21. // Dispose of any resources that can be recreated.
  22. }
  23.  
  24. - (IBAction)editButtonPress:(id)sender {
  25.  
  26. NSBundle *bundle = [NSBundle mainBundle];
  27. NSString *moviePath = [bundle pathForResource:@"YY"
  28. ofType:@"mp4"];
  29.  
  30. //推断设备是否支持编辑视频
  31. if ([UIVideoEditorController canEditVideoAtPath:moviePath]){
  32.  
  33. UIVideoEditorController *videoEditor =
  34. [[UIVideoEditorController alloc] init];
  35.  
  36. videoEditor.delegate = self;
  37. videoEditor.videoPath = moviePath;
  38.  
  39. [self presentViewController:videoEditor animated:YES completion:NULL];
  40.  
  41. } else {
  42. NSLog(@"不能编辑这个视频");
  43. }
  44.  
  45. }
  46.  
  47. - (void)videoEditorController:(UIVideoEditorController *)editor
  48. didSaveEditedVideoToPath:(NSString *)editedVideoPath{
  49.  
  50. [editor dismissViewControllerAnimated:YES completion:NULL];
  51.  
  52. if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(editedVideoPath) ) {
  53. UISaveVideoAtPathToSavedPhotosAlbum(editedVideoPath, self,
  54. @selector(video:didFinishSavingWithError:contextInfo:),
  55. (__bridge void *)(editedVideoPath));
  56. }
  57. }
  58.  
  59. - (void)videoEditorController:(UIVideoEditorController *)editor
  60. didFailWithError:(NSError *)error{
  61. NSLog(@"编辑视频出错");
  62. NSLog(@"Video editor error occurred = %@", error);
  63. [editor dismissViewControllerAnimated:YES completion:NULL];
  64. }
  65.  
  66. - (void)videoEditorControllerDidCancel:(UIVideoEditorController *)editor{
  67. NSLog(@"视频编辑取消");
  68. [editor dismissViewControllerAnimated:YES completion:NULL];
  69. }
  70.  
  71. - (void)video:(NSString *)videoPath
  72. didFinishSavingWithError:(NSError *)error
  73. contextInfo:(NSString *)contextInfo {
  74.  
  75. NSString *title; NSString *message;
  76. if (!error) {
  77. title = @"视频保存";
  78. message = @"视频已经保存到设备的相机胶卷中";
  79. } else {
  80. title = @"视频失败";
  81. message = [error description];
  82. }
  83. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
  84. message:message
  85. delegate:nil
  86. cancelButtonTitle:@"OK"
  87. otherButtonTitles:nil];
  88. [alert show];
  89. }

个人感言,事实上视频我之前做过,也有类似文章,在国外的一些站点上全然也能找到比这更好的更有技术含量的代码和文章,只是总之,算是一个学习记录吧~

原书:http://item.jd.com/11522516.html

&lt;图形图像,动画,多媒体&gt; 读书笔记 --- 录制与编辑视频的更多相关文章

  1. &lt;图形图像,动画,多媒体&gt; 读书笔记 --- AirPlay

    AirPlay技术是之前一直没有接触过的技术,正好这次做一个笔记 共用: 1.能够通过AirPlay将iOS和MAC设备上的视频或音频输出到高清电视上或高保真音响 2.能够通过AirPlay将iOS和 ...

  2. &lt;图形图像,动画,多媒体&gt; 读书笔记 --- 力学行为特性

    UIKit力学行为包括了:重力(UIGravityBehavior),碰撞(UICollisionBehavior),吸附(UIAttachmentBehavior),推(UIPushBehavior ...

  3. &lt;图形图像,动画,多媒体&gt; 读书笔记 --- 音效

    音频多媒体文件主要是存放音频数据信息,音频文件在录制的过程中把声音信号,通过音频编码,变成音频数字信号保存到某种格式文件里.在播放过程中在对音频文件解码,解码出的信号通过扬声器等设备就能够转成音波.音 ...

  4. iOS 图形图像动画 Core Animation

    //Core Animation #define WeakSelf __weak __typeof(self) weakSelf = self #define StrongSelf __strong ...

  5. 《Java并发编程实战》第九章 图形用户界面应用程序界面 读书笔记

    一.为什么GUI是单线程化 传统的GUI应用程序通常都是单线程的. 1. 在代码的各个位置都须要调用poll方法来获得输入事件(这样的方式将给代码带来极大的混乱) 2. 通过一个"主事件循环 ...

  6. 【python下使用OpenCV实现计算机视觉读书笔记3】读写视频文件

    代码例如以下: import cv2 videoCapture = cv2.VideoCapture('car.avi') fps = videoCapture.get(cv2.cv.CV_CAP_P ...

  7. 关东升的《iOS实战:图形图像、动画和多媒体卷(Swift版)》上市了

    关东升的<iOS实战:图形图像.动画和多媒体卷(Swift版)>上市了 承蒙广大读者的厚爱我的<iOS实战:图形图像.动画和多媒体卷(Swift版)>京东上市了,欢迎广大读者提 ...

  8. WPF,Silverlight与XAML读书笔记第三十九 - 可视化效果之3D图形

    原文:WPF,Silverlight与XAML读书笔记第三十九 - 可视化效果之3D图形 说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘> ...

  9. WPF,Silverlight与XAML读书笔记第四十三 - 多媒体支持之文本与文档

    说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. Glyphs对象(WPF,Silverlig ...

随机推荐

  1. Python27天 反射 ,isinstance与ssubclass 内置方法

    所学内容 反射 1.hasattr ( 判断一个属性在对象里有没有 ) -------------------- [对象,字符串属性]本质是:# 判断 ' name ' in obj.__dict__ ...

  2. tp3.2 复合查询or

    tp3.2 复合查询or $where['goods_name'] = array("like","%$q%");$where['goods_sn'] = ar ...

  3. 10.Flask-上下文

    1.1.local线程隔离对象 不用local对象的情况 from threading import Thread request = ' class MyThread(Thread): def ru ...

  4. Spark2.2,IDEA,Maven开发环境搭建附测试

    前言: 停滞了一段时间,现在要沉下心来学习点东西,出点货了. 本文没有JavaJDK ScalaSDK和 IDEA的安装过程,网络上会有很多文章介绍这个内容,因此这里就不再赘述. 一.在IDEA上安装 ...

  5. DeltaFish 校园物资共享平台 第四次小组会议

    一.上周记录汇报 齐天扬 学习慕课HTML至14章.构建之法10-14章 李   鑫 学习制作简易的JSP页面和servlet,看完关于HTML的慕课 陈志锴 学习编制简易JSP页面和servlet, ...

  6. OpenCL C

    OpenCL C OpenCL  简介 opencl C是ISO C99的一个扩展,主要区别如下: 去除了C99的一些特性,如:标准C99头文件,函数指针,递归,变长数组,和位域 增加了一些特性用于并 ...

  7. 编码的来历和使用 utf-8 和GB2312比较

    经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, wordpress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...

  8. RRDtool入门详解

    ---------------原创内容,转载请注明出处.<yaoyao0777@Gmail.com>------------ 一.概述 RRDtool(round-robin databa ...

  9. Metric Learning度量学习:**矩阵学习和图学习

    DML学习原文链接:http://blog.csdn.net/lzt1983/article/details/7884553 一篇metric learning(DML)的综述文章,对DML的意义.方 ...

  10. 读白帽子web安全笔记

    点击劫持 frame buseting if (top.location != location) { top.location = self.location } html5的sandbox属性   ...