2017-06-13 76 views
1

在此無法找到向上最新的答案:AVCaptureDevice貶值至AVCaptureDeviceDiscoverySession使用前置攝像頭

- (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。我怎樣才能正確實現AVCaptureDeviceDiscoverySession這個代碼? 我只想使用前置攝像頭。 謝謝

+0

的可能的複製[如何獲得前置攝像頭,後置攝像頭和音頻AVCaptureDeviceDiscoverySession(https://stackoverflow.com/questions/39894630/how -to-GET-前置攝像頭,背面攝像頭和音頻與 - avcapturedevicediscoverysess) – Yeheshuah

回答

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的深度前置攝像頭基地紅外線,只可用

相關問題