Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the iPhone 4 LED light fire instantly?

I'm currently using the below code to turn on and off my iPhone 4 LED light and it's working great, but the only problem is that every time I turn the LED on there is a slight delay. However it turns off instantly. I need it to fire instantly to implement a strobe like feature, and because it's just more convenient.

I've noticed that in Apple's camera app and many other apps that the LED turns on and off instantaneously when you hit the power button.

I've tried adding some of the objects like "session" and "device" as instance variables to my view controller in order to have the iPhone create those objects at load time, however I haven't had any luck in getting it to work.

I've also tried looking at apples WWDC sample code but I just can't seem to decipher their complex code. Can someone please help me figure this out i've been trying for about 4 days to get this to work.

.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FlashlightViewController : UIViewController {

    AVCaptureSession *torchSession;
}

@property (nonatomic, retain) AVCaptureSession * torchSession;

- (void) toggleTorch;

@end

.m

#import "FlashlightViewController.h"

@implementation FlashlightViewController

@synthesize torchSession;

- (void)dealloc 
{
    [torchSession release];
    [super dealloc];
}

- (void)viewDidLoad 
{
    [self toggleTorch];
    [super viewDidLoad];
}

- (void) toggleTorch 
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if ([device hasTorch] && [device hasFlash])
    {
        if (device.torchMode == AVCaptureTorchModeOff) 
        {
            NSLog(@"It's currently off.. turning on now.");

            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.");
            [torchSession stopRunning];
        }
    }
}
like image 989
cgossain Avatar asked Oct 20 '10 23:10

cgossain


People also ask

Is iPhone flash light LED?

The LED flash on your iPhone, iPad Pro, or iPod touch doubles as a flashlight, so you can get extra light when you need it. There are a few ways you can turn your flashlight on or off. You can ask Siri, or you can use Control Center on an iPhone with Face ID or an iPad Pro.

How do I put the flash back on my iPhone?

Go to Settings > Accessibility > Audio/Visual, then turn on LED Flash for Alerts. To prevent LED flashes when iPhone is in silent mode, turn off Flash on Silent.


2 Answers

Do everything (all the session and device configuration stuff) except the flash configuration block before you want to turn the flash LED on, during app init or view load.

Then just set torch mode on when you want to turn the LED on. Something like:

[self.myDevice lockForConfiguration:nil];
[self.myDevice setTorchMode:AVCaptureTorchModeOn];
[self.myDevice setFlashMode:AVCaptureFlashModeOn];
[self.myDevice unlockForConfiguration];

Make sure that myDevice is a properly configured property during your init.

like image 109
hotpaw2 Avatar answered Sep 30 '22 00:09

hotpaw2


A bit necromantic, but here is a great Library to do it :

LARSTTorch

like image 45
Thomas Besnehard Avatar answered Sep 30 '22 00:09

Thomas Besnehard