Kevin2566
Kevin2566

Reputation: 451

AVCapturePhotoSettings.flashMode not working for setting device's flash mode

I have a controller that lets the user take a picture. I want the flash to activate when the picture is taken, but then immediately deactivate (just like the regular Camera app). However, with my current code, the flash does not turn on at all.

let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
        guard let backCamera = AVCaptureDevice.default(for: AVMediaType.video)
            else {
                print("Unable to access back camera!")
                return
        }
        do {
            if backCamera.hasTorch {
                try backCamera.lockForConfiguration()
                let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
                settings.flashMode = .on
                backCamera.unlockForConfiguration()
            }
        } catch {
            return
        }
        stillImageOutput.capturePhoto(with: settings, delegate: self)

Upvotes: 0

Views: 1404

Answers (1)

valentinv
valentinv

Reputation: 139

This is because you have an outer settings as well and you are using the wrong one.

delete the outer settings and use the inner one :)

    guard let backCamera = AVCaptureDevice.default(for: AVMediaType.video)
        else {
            print("Unable to access back camera!")
            return
    }
    do {
        if backCamera.hasTorch {
            try backCamera.lockForConfiguration()
            let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
            settings.flashMode = .on
            backCamera.unlockForConfiguration()

            stillImageOutput.capturePhoto(with: settings, delegate: self)
        }
    } catch {
        return
    }

Upvotes: 2

Related Questions