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
In case someone else runs into the same problem (I can’t think I am the only one to be unlucky!).
Here is the solution I ended up by finding:
It seems that among those two instructions, the first one has to be run first (any expert on AVFoundation is welcome to make further comments if needed):
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection)
captureSession.stopRunning()
And the fact of firing the flash makes the order somewhat random. To solve that I brought the second instruction inside the completion block of the first one, by doing so making sure the order is always the same.
Then I can use :
captureDevice.flashMode = .On // or .Off or .Auto
and it all works.
Note that it is code to set the mode the flash is using, not code that is fired at each flash lightening (as suggested at the start of this post).
I noticed, configuring the AVCaptureDevice
, while the AVCaptureSession
contained an AVCaptureDeviceInput
reference caused this error. My solution was: before configuring an AVCaptureDevice
object, remove the AVCaptureDeviceInput
reference in the AVCaptureSession
and read it when completed. I would no longer get the error with this approach.
Example:
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:&error];
. .
[self.session removeInput:self.input];
if ([device lockForConfiguration:NULL]) {
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
[device unlockForConfiguration];
}
[self.session addInput:self.input];