以编程方式确定在应用程序iphone中使用哪个摄像头,即正面或背面

时间:2014-06-16 13:55:02

标签: iphone camera

在我的应用程序中,我需要确定当前正在使用哪台摄像机并切换摄像机,即将摄像机切换到前置摄像头我已实施以下代码来自Check which camera is currently in use in iOS Application

AVCaptureDevicePosition currentCameraPosition = [[VideoInputDevice device] position];

    if (currentCameraPosition == AVCaptureDevicePositionBack)
    {
        currentCameraPosition = AVCaptureDevicePositionFront;
    }
    else
    {
        currentCameraPosition = AVCaptureDevicePositionBack;
    }

    AVCaptureDevice *backFacingCamera = nil;
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices)
    {
        if ([device position] == currentCameraPosition)
        {
            backFacingCamera = device;
        }
    }
    NSLog(backFacingCamera ? @"Yes":@"No");

但得错了答案

1 个答案:

答案 0 :(得分:0)

您的代码看起来很好。获得结果后,将值赋给变量

- (void)frontCamera {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
    if ([device position] == AVCaptureDevicePositionFront) {
       //Front camera is used
    }

 }

}