需求:
类似微信音视频通话,有个摄像头按钮切换。
实现:
#import "C2RealTimeCameraView.h"
#import <CoreGraphics/CoreGraphics.h>
#import <CoreVideo/CoreVideo.h>
#import <CoreMedia/CoreMedia.h>
@interface C2RealTimeCameraView() <AVCaptureVideoDataOutputSampleBufferDelegate>
@property (nonatomic, strong) AVCaptureSession *captureSession; // 管理输入输出音视频流
@property (nonatomic, strong) UIImageView *imageView; // 输出图像
// @property (nonatomic, strong) CALayer *customLayer; // 输出图像
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *prevLayer; // 相机预览
@property (nonatomic, strong) dispatch_queue_t sessionQueue;
@end
@implementation C2RealTimeCameraView
- (instancetype)init
{
self = [super init];
if (self) {
self.backgroundColor = kColorFromRGB(0xEFF0F2);
}
return self;
}
- (void)setupCameraWithPosition:(AVCaptureDevicePosition)devicePosition onVideoOrientation:(AVCaptureVideoOrientation)viedoOrientation
{
if (self.captureSession.running == YES){
self.imageView.frame = self.bounds;
self.prevLayer.frame = self.bounds;
return;
}
AVCaptureDevice * testDevice;
// 创建Camera镜头组,实现镜头自动变焦 AVCaptureDeviceTypeBuiltInMicrophone
NSArray<AVCaptureDeviceType> * deviceTypeArr = @[AVCaptureDeviceTypeBuiltInWideAngleCamera];
AVCaptureDeviceDiscoverySession * myDiscoverySesion = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:deviceTypeArr mediaType:AVMediaTypeVideo position:devicePosition];
for (AVCaptureDevice *item in myDiscoverySesion.devices) {
// 找到对应的摄像头
if ([item position] == devicePosition) {
testDevice = item;
break;
}
}
[self createQueue];
// 如果没有找到镜头,就不做操作,防止崩溃
if (testDevice != nil) {
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput
deviceInputWithDevice:testDevice error:nil];
AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES;
[captureOutput setSampleBufferDelegate:self queue:self.sessionQueue];
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureOutput setVideoSettings:videoSettings];
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession addInput:captureInput];
[self.captureSession addOutput:captureOutput];
// dispatch_async(self.sessionQueue, ^{
// //开始运行session
//
// });
// if (self.captureSession.running == NO){
// [self sessionStartRunning];
// }
// FIXME:用CALayer.contents显示有可能会导致内存溢出,程序崩溃。
// self.customLayer = [CALayer layer];
// self.customLayer.frame = self.bounds;
// self.customLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/1.0f, 0, 0, 1);
// self.customLayer.affineTransform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI);
// self.customLayer.contentsGravity = kCAGravityResizeAspect;
// [self.layer addSublayer:self.customLayer];
// 解决录屏图像问题
self.imageView = [[UIImageView alloc] init];
self.imageView.frame = self.bounds;
[self addSubview:self.imageView];
// 相机预览
self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
self.prevLayer.frame = self.bounds;
// 指定屏幕方向
self.prevLayer.connection.videoOrientation = viedoOrientation;
self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.layer addSublayer:self.prevLayer];
}
}
/**
* 创建一个队列,防止阻塞主线程
*/
- (void)createQueue{
dispatch_queue_t sessionQueue = dispatch_queue_create("sessionQueue", DISPATCH_QUEUE_SERIAL);
self.sessionQueue = sessionQueue;
}
- (void)sessionStartRunning {
WEAKSELF
dispatch_async(wkSelf.sessionQueue, ^{
StrongWeakSelf
if (!stSelf.captureSession.running) {
[stSelf.captureSession startRunning];
}
});
}
- (void)sessionStopRunning {
WEAKSELF
dispatch_async(wkSelf.sessionQueue, ^{
StrongWeakSelf
if (stSelf.captureSession.running) {
[stSelf.captureSession stopRunning];
}
});
}
#pragma mark - AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
// 执行此方法有可能会导致内存飙升,程序崩溃。
// [self.customLayer performSelectorOnMainThread:@selector(setContents:) withObject: (__bridge id) newImage waitUntilDone:YES];
// 新图层的输出图像方向
UIImage * image = [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationDown];
CGImageRelease(newImage);
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}
@end
注意事项:
1、创建的时候调用[self.cameraView setupCameraWithPosition:AVCaptureDevicePositionFront onVideoOrientation:AVCaptureVideoOrientationPortrait];方法初始化;
2、点击按钮切换的时候调用sessionStartRunning和sessionStopRunning方法即可。
3、sessionStartRunning切换展示画面会白一下,暂时没找到好的解决办法;
4、不要点击切换才调setupCameraWithPosition方法,某些机型会卡很久才出现摄像头画面;
5、注意多线程和回到主线程处理。