Can't use AVCaptureDevice with a flash

早过忘川 提交于 2019-12-01 22:53:12

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).

Scott Rocca

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!