Toggle on/off flash with AVCam

241 views Asked by At

I'm using Apple's AVCam source code to create a custom camera, I'm trying to toggle flash on/off but its not working. Here is my code, not sure what's wrong. I'm new to AVCam.

- (void) toggleFlash:(id)sender {
    dispatch_async([self sessionQueue], ^{
        AVCaptureDevice *currentVideoDevice = [[self videoDeviceInput] device];
        AVCaptureDevicePosition currentPosition = [currentVideoDevice position];
        if(currentPosition == AVCaptureDevicePositionUnspecified || currentPosition == AVCaptureDevicePositionBack) {
            if([currentVideoDevice hasFlash]) {
                [currentVideoDevice lockForConfiguration:nil];
                [currentVideoDevice setFlashMode:AVCaptureFlashModeOn];
                [currentVideoDevice unlockForConfiguration];
            }
        }
    });
}

Its go through each line in code, and not logs any errors from this but still no luck.

1

There are 1 answers

0
Hemang On BEST ANSWER
- (void) toggleFlash {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch] && [device hasFlash]){
        [device lockForConfiguration:nil];
        [device setTorchMode:!device.torchActive];
        [device setFlashMode:!device.torchActive];
        [device unlockForConfiguration];
    }
}

P.S. In my case, torch/flash is off initially.