Can't use AVCaptureDevice with a flash

后端 未结 2 1335
说谎
说谎 2021-01-21 18:26

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

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 18:56

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

提交回复
热议问题