Reputation: 301
I have a switch that if I activate it, I turn on the camera flash and if you turn off, turn off (default is off)
This is my code:
- (void)viewDidAppear:(BOOL)animated
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO)
return;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerCameraCaptureModeVideo];
picker.allowsEditing = NO;
picker.showsCameraControls = NO;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}
- (IBAction) onChangeSwitch:(id)sender
{
switch(interruptor.on){
case YES:
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
break;
case NO:
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
break;
}
}
Looking online, I've seen the code I have is to turn the flash simply and not to start or stop the torch from the iPhone camera.
I've seen it done with the AVCaptureDevice Turn on torch/flash on iPhone # 3367424 I do not know now how could adapt that to my code.
Does anyone know and gives me a hand?
thanks
Upvotes: 4
Views: 1218
Reputation: 93
- (void)flashLightOn {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device hasFlash] == YES) {
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
}
}
-(void)flashLightOff {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device hasFlash] == YES) {
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}
}
Upvotes: 3
Reputation: 13629
Here is how I turn the light (a.k.a. torch) on & off:
- (void) setTorchOn:(BOOL)isOn
{
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil]; //you must lock before setting torch mode
[device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
I'm pretty sure you'll need to link to the AVFoundation framework.
Upvotes: 2