AVCapturePhotoSettings.flashMode无法用于设置设备的闪光模式

时间:2019-07-25 21:16:47

标签: ios swift avfoundation

我有一个控制器,可以让用户拍照。我希望在拍摄照片时启动闪光灯,但随后立即将其停用(就像常规的“相机”应用程序一样)。但是,使用我当前的代码,闪光灯根本无法打开。

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)

1 个答案:

答案 0 :(得分:0)

这是因为您还有一个外部设置,并且使用的是错误的设置。

删除外部设置并使用内部设置:)

    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
    }