Dimming LED for iPhone 5 Flashlight App in xcode

I am looking to dim the flashlight LED with the slider option. I know that Apple supports this for iOS 6, but I'm not quite sure which code to use. Here is the code I have in the .m file.

-(IBAction)torchOn:(id)sender; { onButton.hidden = YES; offButton.hidden = NO; onView.hidden = NO; offView.hidden = YES; AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn]) { BOOL success = [flashLight lockForConfiguration:nil]; if(success) { [flashLight setTorchMode:AVCaptureTorchModeOn]; [flashLight unlockForConfiguration]; } } } -(IBAction)torchOff:(id)sender; { onButton.hidden = NO; offButton.hidden = YES; onView.hidden = YES; offView.hidden = NO; AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn]) { BOOL success = [flashLight lockForConfiguration:nil]; if(success) { [flashLight setTorchMode:AVCaptureTorchModeOff]; [flashLight unlockForConfiguration]; } } } 

Any suggestions?

+7
source share
3 answers

- (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError

Does what you want. However, from what I see, it is updated only at certain intervals (~ 0.2).

 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchModeOnWithLevel:slider.value error:NULL]; [device unlockForConfiguration]; 

Edit - full example:

Here is the UISlider. You need to add an IBAction output to the slider or add a target programmatically (e.g. me):

 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 40.0f)]; slider.maximumValue = 1.0f; slider.minimumValue = 0.0f; [slider setContinuous:YES]; [slider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slider]; 

Then, in response to the change in the slider:

 - (void)sliderDidChange:(UISlider *)slider { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchModeOnWithLevel:slider.value error:NULL]; [device unlockForConfiguration]; } 
+13
source

AVCaptureDevice setTorchModeOnWithLevel: error: the function is supported only with ios 6.0+ devices. http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVCaptureDevice_Class/Reference/Reference.html

What about iOS 5 devices? What can I do for similar functionality in iOS 5?

0
source

If there are two sliders, because the slider is set to m. of the file, you must delete part of the code [self.view addSubview:slider]; .

0
source

All Articles