AV框架前置摄像头问题

时间:2012-08-27 19:24:42

标签: ios video camera avfoundation

您好我正在使用AV框架尝试访问前置摄像头......

这是我的代码..

+ (AVCaptureDevice *)camera
{
 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

for (AVCaptureDevice *device in devices) {

    if ([device position] == AVCaptureDevicePositionFront) {
        NSLog(@"Device Position is :%i",device.position);
        return device;

    }

}

return nil;
}

NSLog告诉我我在第二位,但我的屏幕上显示的视频仍在后面......有什么建议吗?

1 个答案:

答案 0 :(得分:2)

- (AVCaptureDevice *)frontFacingCameraIfAvailable
{
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

        AVCaptureDevice *captureDevice = nil;

for (AVCaptureDevice *device in videoDevices) 
{
   if (device.position == AVCaptureDevicePositionFron 
      {
            captureDevice = device;
            break;
        }
    }
//  couldn't find one on the front, so just get the default video device.
    if (!captureDevice)
    {
        captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }
    return captureDevice;
}


AVCaptureDevice *captureDevice = nil;  

if(frontCamera == true){  
    captureDevice = [self frontFacingCameraIfAvailable];  
}

captureInput = [AVCaptureDeviceInput   
            deviceInputWithDevice:captureDevice   
            error:nil];