Mathieu
Mathieu

Reputation: 1491

How to know if camera flash is already lit or not (iOS)?

Update : I know how to turn on/off the camera flash. What I want to know is if the camera flash is already lit or not.

I would like to know if camera flash is lit or not on iPhone, but I haven't found any method in UIImagePickerController which allows me to do this. I know we can get the cameraFlashMode. But I want to know if the camera flash is already lit or not.

For example, if the mode is UIImagePickerControllerCameraFlashModeAuto, the camera flash could be lit or not before I take the control, and I want to know the state of camera flash before doing some operations.

Upvotes: 1

Views: 479

Answers (3)

Mehul
Mehul

Reputation: 602

I went through the same issue as yours.

iOS supports two modes - Flash Light & Torch. The code below checks if each is available & then if turns them on or off depending on which one you call. Also it checks if the light is already on/off.

Flash On -

-(void)flashOn {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];

    if ([device hasFlash]) {
        if ([device flashMode] == AVCaptureFlashModeOff) {
            [device setFlashMode:AVCaptureFlashModeOn];
        }
    }

    if ([device hasTorch]) {
        if ([device torchMode] == AVCaptureTorchModeOff) {
            [device setTorchMode:AVCaptureTorchModeOn];
        }
    }

    [device unlockForConfiguration];
  }
}

Flash Off-

-(void)flashOff {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];

    if ([device hasFlash]) {
        if ([device flashMode] == AVCaptureFlashModeOn) {
            [device setFlashMode:AVCaptureFlashModeOff];
        }
    }

    if ([device hasTorch]) {
        if ([device torchMode] == AVCaptureTorchModeOn) {
            [device setTorchMode:AVCaptureTorchModeOff];
        }
    }

    [device unlockForConfiguration];
  }

}

Upvotes: 0

Scott Jenkins
Scott Jenkins

Reputation: 331

Surprising this is really unanswered the first person that answered didn't even answer the question...

   func torchButtonPressed() {
    //
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    do {
        try device.lockForConfiguration()
    } catch {
        return
    }
    if device.torchMode == AVCaptureTorchMode.Off {
        do {
            device.torchMode = AVCaptureTorchMode.On
            try device.setTorchModeOnWithLevel(AVCaptureMaxAvailableTorchLevel)
        } catch {
            print("no torch")
            return
        }

    } else {
        device.torchMode = AVCaptureTorchMode.Off
    }
    device.unlockForConfiguration()
}

Upvotes: 1

Vishnu
Vishnu

Reputation: 2243

You can use following code to find that

#import <AVFoundation/AVFoundation.h>


- (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) {
        NSLog(@"Torch is ON");
    } else {
      NSLog(@"Torch is OFF");

    }
    [device unlockForConfiguration];
}
}
}

Happy Coding...!!!

Upvotes: 0

Related Questions