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
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.
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.
Go to Settings > Accessibility > Audio/Visual, then turn on LED Flash for Alerts.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With