Is it possible to create custom flash behavior for iOS camera

I am trying to mimic the flash behavior of a disposable camera on iPhone.

With a disposable camera, when the capture button is clicked, the flash instantly and quickly goes off once, right as the photo is captured. However, with the iPhone’s camera, when the capture button is pressed, the torch is turned on for roughly a second, and then the sound is produced & torch flashes on & off as the photo is capture.

When using the straightforward Apple API of setting the AVCapturePhotoSettings.flashMode to on:

var photoSettings = AVCapturePhotoSettings()
photoSettings.flashMode = .on

the system default flash behavior is applied. Instead, I would like to create my own custom flash behavior which does not include the torch being turned on for roughly a second before it flashes again in order to mimic a disposable camera.

My idea was to manually toggle the torch either right before or during the AVCapturePhotoOutput.capturePhoto() process.

private let flashSessionQueue = DispatchQueue(label: "flash session queue")

func toggleTorch(_ on: Bool) {
    let device = AVCaptureDevice.default(.builtInDualWideCamera, for: .video, position: .back)!
    do {
        try device.lockForConfiguration()
        device.torchMode = on ? .on : .off
        device.unlockForConfiguration()
    } catch {
        print("Torch could not be used")
    }
}

func photoOutput(_ output: AVCapturePhotoOutput, willBeginCaptureFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
    flashSessionQueue.async {
        self.toggleTorch(true)
    }

    flashSessionQueue.asyncAfter(deadline: .now() + 0.2) {
        self.toggleTorch(false)
    }
}

However, when I do this, the photo is not actually captured, and instead I get either error “**AVFoundationErrorDomain Code=-11800". The flash does occur—the torch turns on and off—but no photo is captured.

Would it even be possible to create a custom flash by manually toggling the torch before / during a camera capture? I have been doing research on AVCaptureMultiCamSession and am also wondering if that could provide a solution.

Is it possible to create custom flash behavior for iOS camera
 
 
Q