Skip to content

Instantly share code, notes, and snippets.

@hirohitokato
Created December 3, 2012 10:26
Show Gist options
  • Save hirohitokato/4194090 to your computer and use it in GitHub Desktop.
Save hirohitokato/4194090 to your computer and use it in GitHub Desktop.
Torch code for iOS6
@property (nonatomic,strong)AVCaptureSession *captureSession;
@property (nonatomic,strong)AVCaptureDevice *device;
@property (weak, nonatomic) IBOutlet UISlider *torchLevelSlider;
- (void)setupTorch
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *d in devices)
if (d.hasTorch && [d isTorchModeSupported:AVCaptureTorchModeOn]) {
self.device = d;
break;
}
if (self.device) {
self.captureSession = [[AVCaptureSession alloc] init];
AVCaptureDeviceInput *videoIn = [[AVCaptureDeviceInput alloc] initWithDevice:_device error:nil];
if ([_captureSession canAddInput:videoIn])
[_captureSession addInput:videoIn];
}
[self torchLevelChanged:self.torchLevelSlider];
}
- (IBAction)torchLevelChanged:(UISlider *)sender {
float value = sender.value;
NSError *error;
[_device lockForConfiguration:&error];
if (value == 0.0f) {
_device.torchMode = AVCaptureTorchModeOff;
} else {
if (value == 1.0f)
value = AVCaptureMaxAvailableTorchLevel;
[_device setTorchModeOnWithLevel:value error:&error];
}
[_device unlockForConfiguration];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment