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

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

我有一个可以让用户拍照的控制器。我希望闪光灯在拍摄照片时激活,但随后立即停用(就像普通的相机应用程序一样)。但是,使用我当前的代码,闪光灯根本不会打开。

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)

这是因为你也有外部设置,但你用错了。

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

    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
    }