Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTorchModeOnWithLevel not working

I am using this method to turn the torch on:

- (void)checkTorch {
    for (AVCaptureDevice *device in [AVCaptureDevice devices]) {

        if ([device position] != AVCaptureDevicePositionBack) {
            NSLog(@"This is the front camera");
            continue; // go to next device
        }

        NSLog(@"This is the back camera");

        if([device hasTorch] == NO){
            NSLog(@"this camera has no torch...");
            continue; // go to next device
        }

        NSLog(@"The camera has a torch");

        if([device isTorchAvailable] == NO){
            NSLog(@"The torch is not available...");
            continue; // go to next device
        }

        NSLog(@"The torch is available");

        NSError *outError;
        NSError *lockError;

        [device lockForConfiguration:&lockError];
        BOOL success = [device setTorchModeOnWithLevel:0.2 error:&outError];
        [device unlockForConfiguration];

        if (lockError)
            NSLog(@"Error locking: %@", lockError.localizedDescription);

        NSLog(@"Torch Level: %0.0f", device.torchLevel);

        if(!success){
            NSLog(@"Could not activate torch: %@", [outError localizedDescription]);
            continue; // go to next device
        }

        NSLog(@"The torch is now active!");
    }
}

Which gives me this output (which is correct):

2014-06-10 08:52:30.677 [291:17894] This is the back camera

2014-06-10 08:52:30.679 [291:17894] The camera has a torch

2014-06-10 08:52:30.679 [291:17894] The torch is available

2014-06-10 08:52:30.682 [291:17894] Torch Level: 0

2014-06-10 08:52:30.683 [291:17894] The torch is now active!

2014-06-10 08:52:30.683 [291:17894] This is the front camera

2014-06-10 08:52:30.684 [291:17894] This is the front camera

However the torch never comes on.

-- I am running beta code however the latest API diffs show no change to this method or related methods so I'm not sure why it would not be working. I've asked on apple dev forums with no help.

like image 366
random Avatar asked Oct 22 '25 15:10

random


1 Answers

I am currently, successfully, using the following code to control the torch on iPhone 5, 6 and 6 Plus:

- (void)setTorchToLevel:(float)torchLevel
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        if (torchLevel <= 0.0) {
            [device setTorchMode:AVCaptureTorchModeOff];
        }
        else {
            if (torchLevel >= 1.0)
                torchLevel = AVCaptureMaxAvailableTorchLevel;
            BOOL success = [device setTorchModeOnWithLevel:torchLevel   error:nil];
        }
        [device unlockForConfiguration];
    }
}
like image 180
mahboudz Avatar answered Oct 25 '25 06:10

mahboudz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!