Problem with flash iphone camera

I have a switch that, if I activate it, turns on the camera flash, and if you turn it off, turn it off (off by default)

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; } } 

In online mode, I saw that the code I have is just to turn on the flash and not start or stop the torch from the iPhone camera.

I saw this with AVCaptureDevice Enable Torch / Flash on iPhone # 3367424 Now I don’t know how to adapt this to my code.

Does anyone know and give me a hand?

thanks

+4
source share
2 answers
 - (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]; } } } 
+3
source

Here's how I turn the lights on and off (aka torch):

 - (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 am sure that you will need to associate with the AVFoundation infrastructure.

+2
source

All Articles