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
Swift 2.0 version:
I've written a Torch plugin that works for Cordova 2.2.0. You can find it here:
https://github.com/tomschreck/iOS-Torch-Plugin
Here's a shorter version you can now use to turn the light on or off:
UPDATE: (March 2015)
With iOS 6.0 and later, you can control the brightness or level of the torch using the following method:
You may also want to monitor the return value (
success
) fromsetTorchModeOnWithLevel:
. 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 toAVCaptureMaxAvailableTorchLevel
will set the level to the highest level that is allowed given the temperature of the torch.From iOS 6.0 and above, toggling torch flash on/off,
P.S. This approach is only suggestible if you don't have on/off function. Remember there's one more option
Auto
. i.e.AVCaptureFlashModeAuto
andAVCaptureTorchModeAuto
. To support auto mode as well, you've keep track of current mode and based on that change mode of flash & torch.This work's very well.. hope it help's someone !