I am having difficult times, for something which I think ought to be simple. I just want to light the flash when taking a picture in my iOS app. And all I tried failed or works only 20 percent.
Here is the code fired to light the flash up:
// Here we have: captureDevice.hasFlash && captureDevice.isFlashModeSupported(.On)
do {try captureDevice.lockForConfiguration()
captureDevice.flashMode = .On
captureDevice.unlockForConfiguration()
} catch let error as NSError {
print("captureDevice.lockForConfiguration FAILED")
print(error.code)
}
I have tried several flavors of the code, by moving the 2 lines inside the try block to the end, or by calling the function containing this code from different points. But in all cases at the time of running this:
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
I get the following error:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x13f079340 {Error Domain=NSOSStatusErrorDomain Code=-16800 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16800), NSLocalizedDescription=The operation could not be completed}
If I then replace the line:
captureDevice.flashMode = .On
by:
captureDevice.torchMode = .On
I get something more or less working (unfortunately rather less), without error, but this is using the torch, so I get timing issues:
Also it is not clear if I need to use this line at some point in my code:
captureSession.commitConfiguration()