3

I need to use the Front camera and have the back light LED turned on in iPhone. how can I do that? I can open the front camera using this code:

- (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;
}
}

and I can turn the led on with this code

- (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];
    }
}
}

But when I open the APP and the front camera appear the led turns off. I need both working at the same time.

Thanks

rendellhb
  • 149
  • 2
  • 9

2 Answers2

0

This is not permitted on iOS with current set of APIs. You can understand this by apple's own camera app.

Even they also turn OFF torch when you toggle to use front camera, and do not show the torch switch in that mode.

bllakjakk
  • 5,045
  • 1
  • 18
  • 28
0

From the documentation check out isFlashAvailableForCameraDevice property of UIImagePickerController to check if the flash is allowed with UIImagePickerControllerCameraDeviceFront property.

Singh
  • 2,151
  • 3
  • 15
  • 30