Turn on the torch / flash on iPhone

I know that the only way to turn on the flash and save it on the iPhone 4 is to turn on the camcorder. However, I'm not too sure about the code. 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 anyone know if this will work, or am I missing nothing? (I don't have iPhone 4, but for testing - just try some of the new APIs).

thank

+44
ios iphone avcapturesession avcapturedevice
Jul 06 '10 at 20:38
source share
9 answers

the lockforConfiguration set in your code where you declare your AVCaptureDevice . This is a property.

 [videoCaptureDevice lockForConfiguration:nil]; 
+15
Jul 09 '10 at 7:31
source share

Here you can use the shorter version to turn the lights 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 brightness or torch level 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 can also track the return value ( success ) from setTorchModeOnWithLevel: You may fail if you try to set the level too high and the torch overheats. In this case, setting the level of AVCaptureMaxAvailableTorchLevel set the level to the highest level that is allowed taking into account the temperature of the torch.

+68
Nov 01 2018-11-11T00:
source share

IWasRobbed's answer is great, except that AVCaptureSession is running in the background. On my iPhone 4s, it consumes about 12% of the processor power in accordance with the device, so my application took about 1% of the battery per minute. In other words, if the device is prepared for AV capture, it's not cheap.

Using the code below, my application requires 0.187% per minute, so the battery life is over 5 times.

This code works fine on any device (tested both on 3GS (without flash) and for 4 s). Tested on 4.3 in the simulator.

 #import <AVFoundation/AVFoundation.h> - (void) turnTorchOn:(BOOL)on { 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; } else { [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; torchIsOn = NO; } [device unlockForConfiguration]; } } } 
+35
Apr 7 '12 at 11:33
source share

See the best answer below: stack overflow




Old answer:

Firstly, in your AppDelegate.h file:

 #import <AVFoundation/AVFoundation.h> @interface AppDelegate : NSObject <UIApplicationDelegate> { AVCaptureSession *torchSession; } @property (nonatomic, retain) AVCaptureSession * torchSession; @end 

Then in your AppDelegate.m file:

 @implementation AppDelegate @synthesize torchSession; - (void)dealloc { [torchSession release]; [super dealloc]; } - (id) init { if ((self = [super init])) { // initialize flashlight // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ if (device.torchMode == AVCaptureTorchModeOff) { NSLog(@"Setting up flashlight for later use..."); AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil]; AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; AVCaptureSession *session = [[AVCaptureSession alloc] init]; [session beginConfiguration]; [device lockForConfiguration:nil]; [session addInput:flashInput]; [session addOutput:output]; [device unlockForConfiguration]; [output release]; [session commitConfiguration]; [session startRunning]; [self setTorchSession:session]; [session release]; } } } } return self; } 

Then anytime you want to enable it, just do the following:

 // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; [device unlockForConfiguration]; } 

And similarly to disable it:

 // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; [device unlockForConfiguration]; } 
+18
Jul 29 '10 at 10:19
source share
 //import fremework in .h file #import <AVFoundation/AVFoundation.h> { AVCaptureSession *torchSession; } @property(nonatomic,retain)AVCaptureSession *torchSession; -(IBAction)onoff:(id)sender; //implement in .m file @synthesize torchSession; -(IBAction)onoff:(id)sender { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]) { if (device.torchMode == AVCaptureTorchModeOff) { [button setTitle:@"OFF" forState:UIControlStateNormal]; 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 { [button setTitle:@"ON" forState:UIControlStateNormal]; [torchSession stopRunning]; } } } - (void)dealloc { [torchSession release]; [super dealloc]; } 
+2
Aug 28 2018-12-12T00:
source share

I wrote a Torch plugin that works for Cordova 2.2.0. You can find it here:

https://github.com/tomschreck/iOS-Torch-Plugin

+2
Jan 24 '13 at 16:40
source share

From iOS 6.0 and above, on / off torch flash,

 - (void) toggleFlash { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; [device setFlashMode:(device.flashActive) ? AVCaptureFlashModeOff : AVCaptureFlashModeOn]; [device setTorchMode:(device.torchActive) ? AVCaptureTorchModeOff : AVCaptureTorchModeOn]; [device unlockForConfiguration]; } } 

PS This approach is only possible if you do not have an on / off function. Think of another Auto option. those. AVCaptureFlashModeAuto and AVCaptureTorchModeAuto . To support auto mode, you also track the current mode, and based on this mode, flash and torch changes.

+2
Nov 25 '14 at 6:20
source share

Swift Version 2.0:

 func setTorchLevel(torchLevel: Float) { self.captureSession?.beginConfiguration() defer { self.captureSession?.commitConfiguration() } if let device = backCamera?.device where device.hasTorch && device.torchAvailable { do { try device.lockForConfiguration() defer { device.unlockForConfiguration() } if torchLevel <= 0.0 { device.torchMode = .Off } else if torchLevel >= 1.0 { try device.setTorchModeOnWithLevel(min(torchLevel, AVCaptureMaxAvailableTorchLevel)) } } catch let error { print("Failed to set up torch level with error \(error)") return } } } 
+2
Aug 30 '15 at 10:25
source share

This work is very good .. hope this helps someone!

 -(IBAction)flashlight:(id)sender { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ if (device.torchMode == AVCaptureTorchModeOff) { [sender setTitle:@"Torch Off" forState:UIControlStateNormal]; AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil]; AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; AVCaptureSession *cam = [[AVCaptureSession alloc] init]; [cam beginConfiguration]; [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; [cam addInput:flashInput]; [cam addOutput:output]; [device unlockForConfiguration]; [cam commitConfiguration]; [cam startRunning]; [self setTorchSession:cam]; } else { [sender setTitle:@"Torch On" forState:UIControlStateNormal]; [_torchSession stopRunning]; } } } 
+1
Aug 21 '15 at 12:21
source share



All Articles