Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on torch/flash on iPhone

I know that the only way to turn on the flash and keep it on on iPhone 4 is by turning the video camera on. I'm not too sure of the code though. Here is what I am trying:

-(IBAction)turnTorchOn {     AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];     AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];     NSError *error = nil;     AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];      if (videoInput) {         [captureSession addInput:videoInput];          AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];         [videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];          [captureSession addOutput:videoOutput];          [captureSession startRunning];          videoCaptureDevice.torchMode = AVCaptureTorchModeOn;     }    } 

Does anybody know if this would work or am I missing anything? (I don't have an iPhone 4 yet to test on -just trying out some of the new API's).

Thanks

like image 540
John Avatar asked Jul 06 '10 20:07

John


People also ask

How do I make my iPhone torch flash?

Open the Settings app on your iPhone. Scroll down and tap on the 'Control Center' option, then on 'Customize Controls'. If you don't already see Flashlight under the 'INCLUDE' list, enable it by tapping on the green colored + sign next to the Flashlight control under the 'MORE CONTROLS' group.

Can you shake your iPhone to turn on the flash light?

Just as the tweak's name implies, ShakeLight lets you activate your iPhone's LED torch by shaking your handset. Likewise, you can shake it again to turn the LED torch back off again when you're finished.

How do I turn on the strobe on iPhone?

Go to Settings > Accessibility > Audio/Visual, then turn on LED Flash for Alerts.

Why is the flash light on my iPhone not working?

Even after you turn off Low Power Mode, you may find that your iPhone's flashlight still doesn't work. This may be simply because your device doesn't have enough battery power left to support the feature at that moment. Therefore, you should plug your iPhone into a power source and start charging it.


1 Answers

Here's a shorter version you can now use to turn the light on or off:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) {     [device lockForConfiguration:nil];     [device setTorchMode:AVCaptureTorchModeOn];  // use AVCaptureTorchModeOff to turn off     [device unlockForConfiguration]; } 

UPDATE: (March 2015)

With iOS 6.0 and later, you can control the brightness or level of the torch using the following method:

- (void)setTorchToLevel:(float)torchLevel {     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];     if ([device hasTorch]) {         [device lockForConfiguration:nil];         if (torchLevel <= 0.0) {             [device setTorchMode:AVCaptureTorchModeOff];         }         else {             if (torchLevel >= 1.0)                 torchLevel = AVCaptureMaxAvailableTorchLevel;             BOOL success = [device setTorchModeOnWithLevel:torchLevel   error:nil];         }         [device unlockForConfiguration];     } } 

You may also want to monitor the return value (success) from setTorchModeOnWithLevel:. You may get a failure if you try to set the level too high and the torch is overheating. In that case setting the level to AVCaptureMaxAvailableTorchLevel will set the level to the highest level that is allowed given the temperature of the torch.

like image 187
mahboudz Avatar answered Sep 28 '22 00:09

mahboudz