AVCaptureDevice折旧到AVCaptureDeviceDiscoverySession使用前置摄像头

时间:2017-06-13 10:49:50

标签: ios objective-c iphone xcode

无法找到最新答案:

func metadataOutput(_ output: AVCaptureMetadataOutput, 
                   didOutput metadataObjects: [AVMetadataObject], 
                        from connection: AVCaptureConnection)

- (void)viewDidLoad { [super viewDidLoad]; AVCaptureSession *session = [[AVCaptureSession alloc] init]; session.sessionPreset = AVCaptureSessionPresetHigh; AVCaptureDevice *device = [self frontCamera]; NSError *error = nil; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; [session addInput:input]; AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; newCaptureVideoPreviewLayer.frame = self.view.bounds; [self.view.layer addSublayer:newCaptureVideoPreviewLayer]; [self.view bringSubviewToFront:_logo]; [session startRunning]; } - (AVCaptureDevice *)frontCamera { NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) { if ([device position] == AVCaptureDevicePositionFront) { return device; } } return nil; } 已弃用,并要求使用NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];。如何正确实现AVCaptureDeviceDiscoverySession到此代码? 我只想使用前置摄像头。 感谢

1 个答案:

答案 0 :(得分:0)

您可以通过代码打击访问前置摄像头。

- (AVCaptureDevice *)frontCamera {
    NSArray *devices;
    if (@available(iOS 10.0, *)) {
        AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInTrueDepthCamera, AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionFront];
        devices = session.devices;
    }else{
        devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    }
    for (AVCaptureDevice *device in devices) {
        if ([device position] == AVCaptureDevicePositionFront) {
            return device;
        }
    }
    return nil;
}

根据Apple's docs

AVCaptureDeviceDiscoverySession可以找到当前可用的捕获设备以及指定设备的监视器可用性。 AVCaptureDeviceTypeBuiltInWideAngleCamera指定普通前置摄像头。 AVCaptureDeviceTypeBuiltInTrueDepthCamera指定深度前置摄像头基础红外线,仅适用于iPhone X.