其实这相当于是一个自定义相机的功能了
github下载链接:https://github.com/SSYSSK/camera2
1、自定义相机
2、视频的捕获预览
3、视频的录制
4、拍照
5、解决了拍照照片翻转90度的问题
6、解决了前置摄像头照片颠倒的问题
关键点:
1、自从iOS10之后,获取图片的输出由AVCaptureStillImageOutput变成了AVCapturePhotoOutput,所以需要做两套适配,输出配置代码:
//6、静态图片输出
if (@available(iOS 10.0, *)) {
self.imageOutput = [[AVCapturePhotoOutput alloc]init];
//输出连接 判断是否可用,可用则添加到输出连接中去
if ([self.captureSession canAddOutput:self.imageOutput])
{
[self.captureSession addOutput:self.imageOutput];
}
}else {
//AVCaptureStillImageOutput 实例 从摄像头捕捉静态图片
self.stillImageOutput = [[AVCaptureStillImageOutput alloc]init];
//配置字典:希望捕捉到JPEG格式的图片
self.stillImageOutput.outputSettings = @{AVVideoCodecKey:AVVideoCodecTypeJPEG};
if ([self.captureSession canAddOutput:self.stillImageOutput])
{
[self.captureSession addOutput:self.stillImageOutput];
}
}
2、拍照功能,要区分iOS10和iOS10之前:
[output capturePhotoWithSettings:settings delegate:self];
// 从流中获取照片
-(void)getPhoto {
// 拍照
if (@available(iOS 10.0, *)) {
AVCapturePhotoOutput * output = (AVCapturePhotoOutput *)self.imageOutput;
AVCapturePhotoSettings * settings = [AVCapturePhotoSettings photoSettings];
// 这一句代码才是执行拍照
[output capturePhotoWithSettings:settings delegate:self];
}else{
AVCaptureStillImageOutput * stillImageOutput = (AVCaptureStillImageOutput *)self.stillImageOutput;
//从 AVCaptureStillImageOutput 中取得 AVCaptureConnection
AVCaptureConnection *connection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
[stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef _Nullable imageDataSampleBuffer, NSError * _Nullable error) {
if (imageDataSampleBuffer != nil) {
NSData * data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc]initWithData:data];
//重点:捕捉图片成功后,将图片传递出去
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
}];
}
}
3、拍照完成的代理回调(iOS10之后才会走这里),这里要注意一个照片翻转的问题:
//前置摄像头拍照会旋转180解决办法
if (self.activeVideoInput.device.position == AVCaptureDevicePositionFront) {
UIImageOrientation imgOrientation = UIImageOrientationLeftMirrored;
image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
}else {
UIImageOrientation imgOrientation = UIImageOrientationRight;
image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
}
#pragma mark AVCapturePhotoCaptureDelegate
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {
if (error) {
NSLog(@"获取图片错误 --- %@",error.localizedDescription);
}
if (photo) {
if (@available(iOS 11.0, *)) {
CGImageRef cgImage = [photo CGImageRepresentation];
UIImage * image = [UIImage imageWithCGImage:cgImage];
NSLog(@"获取图片成功 --- %@",image);
//前置摄像头拍照会旋转180解决办法
if (self.activeVideoInput.device.position == AVCaptureDevicePositionFront) {
UIImageOrientation imgOrientation = UIImageOrientationLeftMirrored;
image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
}else {
UIImageOrientation imgOrientation = UIImageOrientationRight;
image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
}
//重新画一张图片(将时间/个人信息/地址信息画上去)
// self.image = [self drawMarkImage:image];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
} else {
NSLog(@"不是走这个代理方法");
}
}
}