【问题标题】:setTorchModeOnWithLevel from string来自字符串的 setTorchModeOnWithLevel
【发布时间】:2023-03-25 17:17:01
【问题描述】:

我的变量:

var torchLevel : Float = 0.0

我的切换功能:

func toggleFlash() {
        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        if (device.hasTorch){
            device.lockForConfiguration(nil)
            if (device.torchMode == AVCaptureTorchMode.On)
            {
                device.torchMode = AVCaptureTorchMode.Off
                isTorchOn = 0
            }
            else
            {
                device.setTorchModeOnWithLevel(torchLevel, error: nil)
                isTorchOn = 1
            }
            device.unlockForConfiguration()
        }
    }

更改滑块:

@IBAction func brightnessSliderAction(sender: UISlider) {
        var currentBrightnessValue = Int(sender.value)
        torchLevel = currentBrightnessValue
        valueLabel.text = "\(torchLevel)"
    }

代码错误:

torchLevel = currentBrightnessValue

错误信息:

无法将“int”类型的值分配给“Float”类型的值

【问题讨论】:

    标签: swift


    【解决方案1】:

    您需要将 torchLevel 转换为 Float。

    或者让它像这样显式地浮动:

    var torchLevel : Float = 0.0

    【讨论】:

    • 只用torchLevel = sender.value替换孔代码
    • 您还应该注意,torchLevel 必须介于 0.0 和 1.0 之间,因此如果它可能更低或更大,请不要忘记钳制 sender.value。
    • 谢谢。但由于力强度从 0.1 min 到 1.0 max,我如何使用滑块来做到这一点?
    • UISlider 有 minimumValuemaximumValue 为自己说话。
    • 是的,刚刚发现。但是如果滑块在 0 并且我启动手电筒,应用程序崩溃.. 编辑:使用“if value == 0”修复它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多