Created
December 3, 2012 10:26
-
-
Save hirohitokato/4194090 to your computer and use it in GitHub Desktop.
Torch code for iOS6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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