Can't use AVCaptureDevice with a flash

2019-03-02 05:51发布

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

2条回答
祖国的老花朵
2楼-- · 2019-03-02 06:06

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

查看更多
Juvenile、少年°
3楼-- · 2019-03-02 06:08

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];
查看更多
登录 后发表回答