我是靠谱客的博主 自由乌龟,这篇文章主要介绍iOS使用原生AVCapture系列,现在分享给大家,希望可以做个参考。

概述:

可用于音频、二维码、拍照、录制视频 (均可自定义界面)

常见的输出信号:

  • AVCaptureAudioDataOutput 音频输出
  • AVCaptureFileOutput 文本输出
  • AVCaptureMetadataOutput 二维码 条形码…
  • AVCaptureStillImageOutput 拍照
  • AVCaptureMovieFileOutput 录制视频(不能实现暂停录制和定义视频文件类型)
  • AVCaptureVideoDataOutput + AVCaptureAudioDataOutput 录制视频的灵活性更强(能实现暂停录制和定义视频文件类型)

AVCaptureMovieFileOutput输出流实现视频录制

初始化会话层

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-(void)sessionConfiguration{ //初始化一个会话 session = [[AVCaptureSession alloc] init]; [session setSessionPreset:AVCaptureSessionPresetMedium]; //创建视频设备 AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //根据设备创建输入信号 deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; //添加 输出设备 movieFile self.deviceMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; [session beginConfiguration]; //session添加设备输入信号 if ([session canAddInput:deviceInput]) { [session addInput:deviceInput]; } //session添加设备输出信号 if ([session canAddOutput:self.deviceMovieFileOutput]) { [session addOutput:self.deviceMovieFileOutput]; } [session commitConfiguration]; }

创建预览图层

复制代码
1
2
3
4
5
6
7
8
9
10
11
-(void)embedLayerWithView:(UIView *)view{ if (session == nil) { return; } videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session]; //设置图层的大小 videoPreviewLayer.frame = view.bounds; videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; [view.layer addSublayer:videoPreviewLayer]; [session startRunning]; }

录制视频

复制代码
1
2
3
-(void)takePhoto:(NSURL *)fileURL{ [self.deviceMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self]; }

结束录制

复制代码
1
2
3
4
5
6
7
8
-(UIImageView *)finishRecord:(UIView *)view isAnewRecording:(BOOL)anewRecording{ gifImageView = [[UIImageView alloc] initWithFrame:view.bounds]; [view addSubview:gifImageView]; isAnewRecording = anewRecording; //存储是否重新录制 //停止录制(停止录制后做代理方法) [self.deviceMovieFileOutput stopRecording]; return gifImageView; }

拍摄视频保存路径

复制代码
1
2
3
4
5
+(NSString *)getVideoSaveFilePath{ NSString*documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [documentPath stringByAppendingPathComponent:@"video.mp4"]; return filePath; }

会话层启动和关闭

复制代码
1
2
3
4
5
6
7
-(void)startCamera{ [session startRunning]; } -(void)stopCamera{ [session stopRunning]; }

代理方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{ NSLog(@"完成录制"); NSLog(@"outputFileURL = %@",outputFileURL); //**重新录制**// if (isAnewRecording) { //**删除视频文件**// NSFileManager *manager = [NSFileManager defaultManager]; [manager removeItemAtPath:outputFileURL.absoluteString error:nil]; } //**不取消录制**// else{ //**获取视频时长**// AVURLAsset *avUrl = [AVURLAsset URLAssetWithURL:outputFileURL options:nil]; CMTime time = [avUrl duration]; int seconds = ceil(time.value/time.timescale); NSLog(@"seconds = %d",seconds); if ([self.delegate respondsToSelector:@selector(videoDuration:)]) { [self.delegate videoDuration:seconds]; } if ([self.delegate respondsToSelector:@selector(playerVideo:)]) { [self.delegate playerVideo:outputFileURL.absoluteString]; } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是自由乌龟最近收集整理的关于iOS使用原生AVCapture系列的全部内容,更多相关iOS使用原生AVCapture系列内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(128)

评论列表共有 0 条评论

立即
投稿
返回
顶部