iOS自定义视频录制界面
Posted 我来看烟花
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS自定义视频录制界面相关的知识,希望对你有一定的参考价值。
相信在ios应用里面很常用的功能之一就是视频录制了,但是往往系统的录制界面不是我们想要的,所以这时候就得自定义视频录制界面了.当然对很多人来讲的,AVFoundation这个库不是很熟.下面就一步步来实现一个自定义界面把!
创建UI以及相机采集工作
NSError *error = nil;
self.session = [[AVCaptureSession alloc] init];
//可以根据自己的需求来决定预览层的画质
_session.sessionPreset = AVCaptureSessionPresetLow;
AVCaptureDevice *frontCamera = nil;
AVCaptureDevice *backCamera = nil;
NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *camera in cameras)
if (camera.position == AVCaptureDevicePositionFront)
frontCamera = camera;
else
backCamera = camera;
if (!backCamera)
self.isCameraSupported = NO;
return;
else
self.isCameraSupported = YES;
if ([backCamera hasTorch])
self.isTorchSupported = YES;
else
self.isTorchSupported = NO;
if (!frontCamera)
self.isFrontCameraSupported = NO;
else
self.isFrontCameraSupported = YES;
[backCamera lockForConfiguration:nil];
if ([backCamera isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure])
[backCamera setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
[backCamera unlockForConfiguration];
// 用device对象创建一个设备对象input,并将其添加到session
self.videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
if (!_videoDeviceInput)
return;
[_session addInput:_videoDeviceInput];
AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio] error:nil];
if (!audioDeviceInput)
_output = [[AVCaptureVideoDataOutput alloc] init];
[_session addOutput:_output];
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[_output setSampleBufferDelegate:self queue:queue];
//这里作者我需要的是480*480的视频 所有先取640x480,后期通过裁剪来获取到480*480的 画质越高处理的速度相对较慢,所以根据自己的需求来定义
_session.sessionPreset = AVCaptureSessionPreset640x480;
dispatch_async(dispatch_get_main_queue(), ^
self.preViewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_preViewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[_session commitConfiguration];
// 启动session
[_session startRunning];
self.preViewLayer.frame = CGRectMake(0, 0, MYSCREENWIDTH, MYSCREENWIDTH*4/4);
//preview是一个UIView,用来加载相机的预览层
[preview.layer addSublayer:self.preViewLayer];
);
获取视频流
#pragma mark 获取视频流
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
//在这里你可以获取到视频采集到的视频帧音频帧,具体要做什么操作 旋转啊 裁剪啊 之类的 就可以根据项目需求来决定
以上是关于iOS自定义视频录制界面的主要内容,如果未能解决你的问题,请参考以下文章