You can use the method below to turn the camera on and off.
- (void)toggleFlashlight { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if (device.torchMode == AVCaptureTorchModeOff) { // Create an AV session AVCaptureSession *session = [[AVCaptureSession alloc] init]; // Create device input and add to current session AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil]; [session addInput:input]; // Create video output and add to current session AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; [session addOutput:output]; // Start session configuration [session beginConfiguration]; [device lockForConfiguration:nil]; // Set torch to on [device setTorchMode:AVCaptureTorchModeOn]; [device unlockForConfiguration]; [session commitConfiguration]; // Start the session [session startRunning]; // Keep the session around [self setAVSession:session]; [output release]; } else { [AVSession stopRunning]; [AVSession release], AVSession = nil; } }
You can also use the following method along with displaying the camera,
- (void) toggleFlashlight { // check if flashlight available Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; if (device.torchMode == AVCaptureTorchModeOff) { [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; } else { [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; } [device unlockForConfiguration]; } } }
A source
source share