Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>See a better answer below:</strong> <a href="https://stackoverflow.com/a/10054088/308315">https://stackoverflow.com/a/10054088/308315</a></p> <hr> <p><strong>Old answer:</strong></p> <p>First, in your AppDelegate .h file:</p> <pre><code>#import &lt;AVFoundation/AVFoundation.h&gt; @interface AppDelegate : NSObject &lt;UIApplicationDelegate&gt; { AVCaptureSession *torchSession; } @property (nonatomic, retain) AVCaptureSession * torchSession; @end </code></pre> <p>Then in your AppDelegate .m file:</p> <pre><code>@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] &amp;&amp; [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; } </code></pre> <p>Then anytime you want to turn it on, just do something like this:</p> <pre><code>// 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]; } </code></pre> <p>And similar for turning it off:</p> <pre><code>// 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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload