AVCapturePhotoOutput自定義簡單相機

在iOS 10之前,自定義相機一般使用AVCaptureStillImageOutput實現(xiàn)。但是AVCaptureStillImageOutput在iOS 10以后被棄用了。


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

所以我們來使用AVCapturePhotoOutput來實現(xiàn)自定義簡單相機,AVCapturePhotoOutput 的功能自然會更加強大,不僅支持簡單JPEG圖片拍攝,還支持Live照片和RAW格式拍攝。

使用:
首先初始化:按照需要參數(shù)初始化就行了,和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];

實現(xiàn)照片獲取:
AVCaptureStillImageOutput中使用captureStillImageAsynchronouslyFromConnection 在bolck中直接可以獲取到圖片,AVCapturePhotoOutput需要實現(xiàn)AVCapturePhotoCaptureDelegate協(xié)議,在協(xié)議中獲取。
獲取當前屏幕圖片輸出:

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

實現(xiàn)AVCapturePhotoCaptureDelegate協(xié)議,并在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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,319評論 25 708
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,616評論 4 61
  • 這周《通往財富自由之路》的文章主題是為何一定要寫作,笑來老師給出了一個他認為不應該拒絕的理由: 寫作是“把自己的同...
    段兔子不在家閱讀 540評論 1 1
  • 這么愉悅地欣賞這小城,畫廊,時間一晃就過去。晚上的重頭戲是酒莊品酒晚筵,得趕快回去換裝啦。
    阿里巴巴他娘閱讀 406評論 0 0

友情鏈接更多精彩內容