Turn on flash and front camera simultaneously on iPhone

I need to use the front camera and turn on the backlight LED in the iPhone. How should I do it? I can open the front camera with 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."
                                          otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;


and i can turn on the led 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 APP and the front camera appears, the LED turns off. I need to work at the same time.



source to share

2 answers

This is not allowed on iOS with the current set of APIs. You can figure it out with Apple's own camera app.

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



From the documentation check the property of the isFlashAvailableForCameraDevice

UIImagePickerController property to check if the use of flash is allowed using the property UIImagePickerControllerCameraDeviceFront




All Articles