-4

Can anyone show me an example of how to use the following method:

 (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError
Tieme
  • 62,602
  • 20
  • 102
  • 156
thokthak
  • 41
  • 5
  • 2
    Stack Overflow is not your personal research assistant. Read the faq please. –  Oct 29 '12 at 20:02

1 Answers1

2

Check out Apple's reference here: AVCaptureDevice

First import AVFoundation and add it to the build phases.

#import <AVFoundation/AVFoundation.h>

Then use following code:

//Get all devices (front and back camera)
for (AVCaptureDevice *device in [AVCaptureDevice devices]) {

    if ([device position] != AVCaptureDevicePositionBack) {
        NSLog(@"This is the front camera");
        continue; // go to next device
    }

    NSLog(@"This is the back camera");

    if([device hasTorch] == NO){
        NSLog(@"this camera has no torch...");
        continue; // go to next device
    }

    NSLog(@"The camera has a torch");

    if([device isTorchAvailable] == NO){
        NSLog(@"The torch is not available...");
        continue; // go to next device
    }

    NSLog(@"The torch is available");

    //get device dependent maximum (between 0 and 1)
    float level = AVCaptureMaxAvailableTorchLevel;

    NSError* outError;
    BOOL success = [device setTorchModeOnWithLevel:level error:&outError];

    if(!success){
        NSLog(@"Could not activate torch: %@", [outError localizedDescription]);
        continue; // go to next device
    }

    NSLog(@"The torch is now active!");
}
Tieme
  • 62,602
  • 20
  • 102
  • 156