How to turn the iPhone camera flash on/off?
Asked Answered
A

2

36

How can I turn the iPhone's LED camera flash on/off programatically?

Array answered 4/5, 2011 at 11:46 Comment(1)
Dont forget to add AVFoundation.framework also...Pentahedron
B
82
#import <AVFoundation/AVFoundation.h>

...

- (void) turnTorchOn: (bool) on {

    // check if flashlight available
    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];
                //torchIsOn = YES; //define as a variable/property if you need to know status 
            } else {
                [device setTorchMode:AVCaptureTorchModeOff];
                [device setFlashMode:AVCaptureFlashModeOff];
                //torchIsOn = NO;            
            }
            [device unlockForConfiguration];
        }
    } }
Brighten answered 7/4, 2012 at 11:17 Comment(7)
Where do you declare torchIsOn ?Fermanagh
@YassineHoussni you can define it either as an iVar in the class name or a property if you need getter and settersIsomeric
@Odelya yeah I already found the solution, I have defined it as a property, thanks though!Fermanagh
You could replace the iVar with device.torchActive.Volding
I better way to check for a class is: if ([AVCaptureDevice class]) { ... } // if you can the SDK that has a class but you're not sure the class exists on the device.Cleocleobulus
for me this just turns the flashlight on my phone on, it doesn't just turn the flash on at the moment a photo is taken in my AVCaptureSession.Montez
I' using these solution for my C++ call and it works pretty fine! Thanks!Girondist
S
25

I combined the timer with the above code.it worked for me...

 - (void)viewDidLoad
        {
         [super viewDidLoad];

         myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self                    selector:@selector(toggleFlashlight) userInfo:nil repeats:YES];
        // Do any additional setup after loading the view from its nib.
        }
       - (void) toggleFlashlight
       {

    // check if flashlight available
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch] && [device hasFlash]){

            [device lockForConfiguration:nil];
            if (device.torchMode == AVCaptureTorchModeOff) 
            {
                [device setTorchMode:AVCaptureTorchModeOn];
                [device setFlashMode:AVCaptureFlashModeOn];
                //torchIsOn = YES;
            }
            else 
            {
                [device setTorchMode:AVCaptureTorchModeOff];
                [device setFlashMode:AVCaptureFlashModeOff];
               // torchIsOn = NO;            
            }
            [device unlockForConfiguration];
        }
    } }
Select answered 9/7, 2012 at 6:7 Comment(1)
Easier to just put it in viewDidAppear.Balalaika

© 2022 - 2025 — McMap. All rights reserved.