- 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;
- }
来源: http://www.phpxs.com/code/1005438/