在 UIImagePickerController 中启用基于活动设备的 flash?

Enabling flash based on active device in UIImagePickerController?

我有一个通过 UIImagePickerController.cameraOverlayView 提供控件的视图。对于这些控件,我只想根据活动捕获设备的当前功能启用它们。例如,如果设备具有该功能,则仅启用 'torch'。

查看 UIImagePickerController 它有一个 cameraDevice 属性,但这只是一个枚举,所以没有帮助。

我当前的代码(基于堆栈溢出其他地方的代码):

func toggleTorch(on: Bool) -> Bool {
    var success = false
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    if device.hasTorch {
        do {
            try device.lockForConfiguration()

            if on == true {
                device.torchMode = .On
            } else {
                device.torchMode = .Off
            }

            device.unlockForConfiguration()
            success = true
        } catch {
            debugPrint("Torch could not be used")
        }
    } else {
        debugPrint("Torch is not available")
    }
    return success
}

我希望能够让设备对应当前UIImagePickerController的活动设备?有办法吗?

看来我错过了 UIImagePickerController 已经提供了我需要的功能这一事实。

查看当前设备是否有闪光灯:

UIImagePickerController.isFlashAvailableForCameraDevice(cameraController?.cameraDevice)

并设置模式(auto/on/off):

cameraController?.cameraFlashMode = UIImagePickerControllerCameraFlashMode.Auto