ios - 如何打开/关闭 iPhone 相机闪光灯 swift 2?

标签 ios iphone flash swift2

我在寻找如何打开/关闭 iPhone 的相机闪光灯,我发现了这个:

@IBAction func didTouchFlashButton(sender: AnyObject) {
    let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

// check if the device has torch
if avDevice.hasTorch {
    // lock your device for configuration
    avDevice.lockForConfiguration(nil)
    // check if your torchMode is on or off. If on turns it off otherwise turns it on
    if avDevice.torchActive {
        avDevice.torchMode = AVCaptureTorchMode.Off
    } else {
        // sets the torch intensity to 100%
        avDevice.setTorchModeOnWithLevel(1.0, error: nil)
    }
    // unlock your device
    avDevice.unlockForConfiguration()
    }
}

我确实遇到了 2 个问题,其中一个在线:

avDevice.lockForConfiguration(nil)

和另一个在线:

avDevice.setTorchModeOnWithLevel(1.0, error:nil)

都和异常处理有关,不知道怎么解决。

最佳答案

@IBAction func didTouchFlashButton(sender: UIButton) {
    let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    // check if the device has torch
    if avDevice.hasTorch {
        // lock your device for configuration
        do {
            let abv = try avDevice.lockForConfiguration()
        } catch {
            print("aaaa")
        }

        // check if your torchMode is on or off. If on turns it off otherwise turns it on
        if avDevice.torchActive {
            avDevice.torchMode = AVCaptureTorchMode.Off
        } else {
            // sets the torch intensity to 100%
            do {
                let abv = try avDevice.setTorchModeOnWithLevel(1.0)
            } catch {
                print("bbb")
            }
        //    avDevice.setTorchModeOnWithLevel(1.0, error: nil)
        }
        // unlock your device
        avDevice.unlockForConfiguration()
    }
}

关于ios - 如何打开/关闭 iPhone 相机闪光灯 swift 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585353/

相关文章:

ios - 为什么 9 月的月份会在我的时差方法中增加五个小时?

ios - CoreData删除保存错误

Safari 上的 Javascript 屏幕方向

iphone - #error 1107 在 iPhone 上运行 flash 游戏时,使用 Adob​​e Air

ios - 监控指定时间内UITextField文本变化

ios - 用于固定 UILabel 高度的字符串

ios - Apple Accelerate vDSP fft vs DFT 和缩放因子

javascript - iOS UIAutomation UIAElement.isVisible() 抛出陈旧的响应?

apache-flex - 当 Flex 应用程序无法以指定的帧速率运行时会发生什么?

javascript - 在 Joomla 上集成 Flash 组件,无需在每个页面上重新加载它