-1

I have some code going with a flashlight button which can be turned on and off but it starts out being on by default, which it is of course not supposed to. Do you guys have any ideas on how to easily change this?

if (!FrontCamera) {

    if ([backCamera hasFlash]){
        [backCamera lockForConfiguration:nil];
        if (self.flashToggleButton.selected)
            [backCamera setFlashMode:AVCaptureFlashModeOn];
        else
            [backCamera setFlashMode:AVCaptureFlashModeOff];
        [backCamera unlockForConfiguration];

        self.flashToggleButton.selected = NO;
    }
    else{
        if ([backCamera isFlashModeSupported:AVCaptureFlashModeOff]) {
            [backCamera lockForConfiguration:nil];
            [backCamera setFlashMode:AVCaptureFlashModeOff];
            [backCamera unlockForConfiguration];
        }
        [self.flashToggleButton setEnabled:NO];
    }

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
    if (!input) {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];
}

if (FrontCamera) {
    [self.flashToggleButton setEnabled:NO];
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&error];
    if (!input) {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];
}
AppCodinz
  • 1
  • 5
  • FYI - you probably want all of the "torch" related methods to create a flashlight. The "flash" methods are for having a light that flashes briefly. The "torch" is for the LED light that can stay lit up for some time. – rmaddy Aug 11 '14 at 16:10
  • I am using the torch, yes. It's not for flashing, it's just making a "torch" when you take a photo. The problem is that "torch" button is automatically on and it should be off by default. Then you have the option to turn it on of course. – AppCodinz Aug 11 '14 at 16:13
  • Where is this code? Are you loading your view from a xib? If so, the view may be loading and displaying, and at some later point, your enable/disable code is running. Try setting the button to disabled in `-viewDidLoad`. – Zev Eisenberg Aug 12 '14 at 14:16
  • @ZevEisenberg I will try that out... 2 sec – AppCodinz Aug 12 '14 at 15:39
  • It is extremely close to working now. The last problem is that when I switch to the front camera (which doesn't have flash of course) and then switch back, the flash button is now not active (can't activate or deactivate it because it is greyed out). It is primarily greyed out when the front camera is on of course. – AppCodinz Aug 12 '14 at 16:28
  • That's because in your `if (FrontCamera) {` block, you are setting the button's `enabled` property to `NO`. The `selected` property lets you set a different appearance for selected vs deselected, and toggle between them. The `enabled` property dictates whether or not the button allows taps. – Zev Eisenberg Aug 12 '14 at 16:40
  • (You can also set a different appearance for enabled/disabled.) – Zev Eisenberg Aug 12 '14 at 16:40
  • It's basically working. Not many people would switch to the front camera and then switch back and then put on the flash, hehe :) Well could you provide a piece of code that could solve all of this anyway? Thanks for all your help, Zev! – AppCodinz Aug 12 '14 at 16:58

1 Answers1

0

Are you using the selected state of the button? If so, try:

self.flashToggleButton.selected = NO;
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82