0

I am having trouble firing the flash in an iOS app using the camera to take pictures.

Here is the function I call when I want to make the flash work:

func litFlash() {
    if !captureDevice.hasFlash || !captureDevice.isFlashModeSupported(.On) {return}

    // We now have: captureDevice.hasFlash && captureDevice.isFlashModeSupported(.On)
    do {try captureDevice.lockForConfiguration()
    } catch let error as NSError {
        print("captureDevice.lockForConfiguration FAILED")
        print(error.code)
    }
    captureDevice.torchMode = .On
    //captureDevice.flashMode = .On // Strangely enough, this produces a crash.
    captureDevice.unlockForConfiguration()
    //captureSession.commitConfiguration()
}

This is where I call the function:

    if fireFlashFlag {litFlash()}

    if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
        stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
            (imageDataSampleBuffer, error) -> Void in
    ………

It works for about 20%, for the rest I get a flash but it is not synchronised with the picture taking. The flash appear a fraction of a second too early.

Hoping someone can point me into the right direction. Thanks in advance for any relevant piece of information.

Michel
  • 10,303
  • 17
  • 82
  • 179
  • Have you tried toggling the flash on before you start call the function to take a picture, and toggling it off after the function is over instead of doing it all in the middle? – Charles Truluck Mar 18 '16 at 16:52
  • Why are you trying to manually turn the backlight on instead of relying on the built-in flash functionality? – David Berry Mar 18 '16 at 16:52
  • To Charles Truluck: By "before you start call the function to take a picture"; do you mean for example before captureSession.startRunning()? I tried and it lights also while I am focusing. – Michel Mar 18 '16 at 17:20
  • To David Berry: This may be what I should do. How can I do that? This is precisely what I would like to know. – Michel Mar 18 '16 at 17:22
  • See the answer to this question: http://stackoverflow.com/questions/36097389/cant-use-avcapturedevice-with-a-flash – Michel Mar 20 '16 at 09:12

0 Answers0