How to access camera flash on UIImagePickerController?

10,686

Solution 1

You can use this. Basically call 'toggleTorch' when you want to turn the flash on or off. Hopefully this is what you were looking for.

- (void) toggleTorch {

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if ([device hasTorch] && [device hasFlash]){

        if (device.torchMode == AVCaptureTorchModeOff) {

            NSLog(@"It's currently off.. turning on now.");

            [power setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];

            AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
            AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

            AVCaptureSession *session = [[AVCaptureSession alloc] init];

            [session beginConfiguration];
            [device lockForConfiguration:nil];

            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];

            [session addInput:flashInput];
            [session addOutput:output];

            [device unlockForConfiguration];

            [output release];

            [session commitConfiguration];
            [session startRunning];

            [self setTorchSession:session];
            [session release];
        }
        else {

            NSLog(@"It's currently on.. turning off now.");

            [power.imageView setImage:[UIImage imageNamed:@"[email protected]"]];

            [torchSession stopRunning];

        }

    }

}

-(IBAction)powerBtn
{
    [self toggleTorch];
}

Solution 2

-(void)flashSelected
{

if (PickerController.cameraFlashMode == 
UIImagePickerControllerCameraFlashModeOff) {

    if ([UIImagePickerController 
isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear ])

    {
        PickerController.cameraFlashMode = 
UIImagePickerControllerCameraFlashModeOn;
    }
 }
else
 {
    PickerController.cameraFlashMode = 
UIImagePickerControllerCameraFlashModeOff;
 }     
}

alternately..

-(void)_flashToggle
{
if (! [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear ])
    return;

if (PickerController.cameraFlashMode == UIImagePickerControllerCameraFlashModeOff)
    PickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
else
    PickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;  
}
Share:
10,686
Aluminum
Author by

Aluminum

I'm an iOS developer and these are the most famous apps I have made on the App Store: Immobiliare.it - https://appsto.re/i6YB8tb SMART Mobile - https://appsto.re/i6LF4ts TIMVision - https://appsto.re/i6Lb7j3 Italo - https://appsto.re/i6SG88W YouDaft - https://appsto.re/i6Bv7FB iSkrillex - https://appsto.re/i6B57sP I am always looking forward on how to make better apps using Objective-C and Swift and StackOverflow really helps me out with this. Passionate about Apple , iOS and OS X, Photography [I own a 550D/Rebel T2i], Design, Music and Movies. Have a good day! 😊

Updated on June 09, 2022

Comments

  • Aluminum
    Aluminum almost 2 years

    I would like to know how to switch on the camera flash on the iPhone 4 with UIImagePickerController.

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerCameraDeviceFront] == YES)
    {
        /// What code here ///
    }
    
    else
    {
        NoFlash = [[UIAlertView alloc] initWithTitle:@"Uh-Oh"
                                             message:@"Your device doesn't have a flash camera"
                                            delegate:nil
                                   cancelButtonTitle:@"mhmm, OK"
                                   otherButtonTitles:nil];
        NoFlash.delegate = self;
        [NoFlash show];
        [NoFlash release];
    }
    

    }

    I already read the UIImagePickerController Class Reference web page here: http://bit.ly/cdAhhB but I didn't find the answer. Can someone please help me?

    Thanks

  • Fattie
    Fattie about 10 years
    this is for the torch, and has nothing to do with the flash. Simply change PickerController.cameraFlashMode for the flash.