在iPhone上同时打开Flash和Front相机

时间:2014-10-03 04:11:33

标签: ios iphone camera flashlight front-camera

我需要使用前置摄像头并在iPhone中打开背光LED。 我怎样才能做到这一点? 我可以使用以下代码打开前置摄像头:

- (void) turnCameraOn {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    imagePicker.showsCameraControls = YES;

    [self presentViewController:imagePicker animated:YES completion:nil];

} else {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera unavaliable"
                                                    message:@"Unable to find camera on your device."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;
}
}

我可以使用此代码打开指示灯

- (void) turnTorchOn: (bool) on {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch] && [device hasFlash]){

        [device lockForConfiguration:nil];

        if (on) {
            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];
        } else {
            [device setTorchMode:AVCaptureTorchModeOff];
            [device setFlashMode:AVCaptureFlashModeOff];
        }
        [device unlockForConfiguration];
    }
}
}

但是当我打开APP并且前置摄像头出现时,LED指示灯熄灭。 我需要同时工作。

由于

2 个答案:

答案 0 :(得分:0)

iOS上不允许使用当前的API集。 你可以通过苹果自己的相机应用程序来理解这一点。

即使他们在切换到使用前置摄像头时也会关闭手电筒,并且在该模式下不显示手电筒开关。

答案 1 :(得分:0)

从UIImagePickerController的documentation签出isFlashAvailableForCameraDevice属性中检查闪存是否允许UIImagePickerControllerCameraDeviceFront属性。