如何开启/关闭iPhone相机闪光灯?

Rob*_*uez 3 iphone flash ios 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()
    }
}
Run Code Online (Sandbox Code Playgroud)

我确实得到了2个问题,一个在线:

avDevice.lockForConfiguration(nil)
Run Code Online (Sandbox Code Playgroud)

和另一个在线:

avDevice.setTorchModeOnWithLevel(1.0, error:nil)
Run Code Online (Sandbox Code Playgroud)

它们都与异常处理有关,但我不知道如何解决它们.

Iva*_*vov 12

@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()
    }
}
Run Code Online (Sandbox Code Playgroud)