AVFoundation 使用基础

目标:实现一个最简单的视频流

AVCaptureSession 会话管理者
_session = [[AVCaptureSession alloc] init];
视频输入设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil];
    if ([_session canAddInput:input]) {
        [_session addInput:input];
    }
视频输出设备
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    if ([_session canAddOutput:output]) {
        [_session addOutput:output];
    }
识别类型,代理
    output.metadataObjectTypes = output.availableMetadataObjectTypes;
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
PreviewLayer
_preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_session];
_preview.frame = self.view.bounds;
[self.view.layer insertSublayer:_preview atIndex:0];
回调方法
  • (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection {

}

你可能感兴趣的:(AVFoundation 使用基础)