1

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.

Javier
  • 12,100
  • 5
  • 46
  • 57
Mathieu
  • 1,491
  • 5
  • 17
  • 37

3 Answers3

1

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()
}
Scott Jenkins
  • 321
  • 2
  • 7
0

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...!!!

Vishnu
  • 2,243
  • 2
  • 21
  • 44
  • 1
    Hi, Vishnu. Thanks for you reply, but I don't quite understand you get the state of flash. If (on) { @"Torch is ON"}, but the "on" here is passed in...Another point is that, the name of the method, turnTorchOn, it seems that this method should be used to turn on/off the flash, not get the on/off state of the flash. Am I right? – Mathieu Mar 04 '13 at 14:35
0

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

}
Mehul
  • 572
  • 4
  • 17