AVCapturePhotoOutput自定义简单相机

在iOS 10之前,自定义相机一般使用AVCaptureStillImageOutput实现。但是AVCaptureStillImageOutput在iOS 10以后被弃用了。


62B55636-A7E0-4F36-B88C-FFA28C38749C.png

所以我们来使用AVCapturePhotoOutput来实现自定义简单相机,AVCapturePhotoOutput 的功能自然会更加强大,不仅支持简单JPEG图片拍摄,还支持Live照片和RAW格式拍摄。

使用:
首先初始化:按照需要参数初始化就行了,和AVCaptureStillImageOutput差别不大。直接上代码:

    self.session = [AVCaptureSession new];
    [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    
    NSArray *devices = [NSArray new];
    devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if (isBack) {
            if ([device position] == AVCaptureDevicePositionBack) {
                _device = device;
                break;
            }
        }else {
            if ([device position] == AVCaptureDevicePositionFront) {
                _device = device;
                break;
            }
        }
    }
    
    NSError *error;
    self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:&error];
    if ([self.session canAddInput:self.input]) {
        [self.session addInput:self.input];
    }
    
    self.imageOutput = [[AVCapturePhotoOutput alloc] init];
    NSDictionary *setDic = @{AVVideoCodecKey:AVVideoCodecJPEG};
    _outputSettings = [AVCapturePhotoSettings photoSettingsWithFormat:setDic];
    [self.imageOutput setPhotoSettingsForSceneMonitoring:_outputSettings];
    [self.session addOutput:self.imageOutput];
    self.preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    [self.preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [self.preview setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self.layer addSublayer:self.preview];
    [self.session startRunning];

实现照片获取:
AVCaptureStillImageOutput中使用captureStillImageAsynchronouslyFromConnection 在bolck中直接可以获取到图片,AVCapturePhotoOutput需要实现AVCapturePhotoCaptureDelegate协议,在协议中获取。
获取当前屏幕图片输出:

[self.imageOutput capturePhotoWithSettings:_outputSettings delegate:self];

实现AVCapturePhotoCaptureDelegate协议,并在didFinishProcessingPhotoSampleBuffer方法中获取图片:

- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(nullable CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(nullable CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(nullable AVCaptureBracketedStillImageSettings *)bracketSettings error:(nullable NSError *)error {
    
    NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
    UIImage *image = [UIImage imageWithData:data];
    
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

这样就获取到图片了。
最后附Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 这周《通往财富自由之路》的文章主题是为何一定要写作,笑来老师给出了一个他认为不应该拒绝的理由: 写作是“把自己的同...
    段兔子不在家阅读 461评论 1 1
  • 这么愉悦地欣赏这小城,画廊,时间一晃就过去。晚上的重头戏是酒庄品酒晚筵,得赶快回去换装啦。
    阿里巴巴他娘阅读 323评论 0 0