首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何打开/关闭iPhone相机闪光灯swift 2?

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

Stack Overflow用户
提问于 2015-11-08 01:02:00
回答 5查看 7.4K关注 0票数 5

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

代码语言:javascript
复制
@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()
    }
}

我确实收到了两个问题,其中一个是:

代码语言:javascript
复制
avDevice.lockForConfiguration(nil)

另一个在线上:

代码语言:javascript
复制
avDevice.setTorchModeOnWithLevel(1.0, error:nil)

这两个问题都与异常处理有关,但我不知道如何解决它们。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-11-16 23:05:18

代码语言:javascript
复制
@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()
    }
}
票数 15
EN

Stack Overflow用户

发布于 2018-09-18 03:30:26

Swift 4版本,改编自Ivan Slavov的答案。如果你想变得花哨,"TorchMode.auto“也是一个选择。

代码语言:javascript
复制
    @IBAction func didTouchFlashButton(_ sender: Any) {
    if let avDevice = AVCaptureDevice.default(for: AVMediaType.video) {
        if (avDevice.hasTorch) {
            do {
                try avDevice.lockForConfiguration()
            } catch {
                print("aaaa")
            }

            if avDevice.isTorchActive {
                avDevice.torchMode = AVCaptureDevice.TorchMode.off
            } else {
                avDevice.torchMode = AVCaptureDevice.TorchMode.on
            }
        }
        // unlock your device
        avDevice.unlockForConfiguration()
    }
}
票数 4
EN

Stack Overflow用户

发布于 2015-11-18 15:20:22

由于某些原因,"avDevice.torchActive“总是假的,即使在手电筒打开的时候也是如此,这使得它无法关闭,但我通过声明一个布尔值来修复它,布尔值最初设置为false,每次打开闪光灯时,布尔值都设置为true。

代码语言:javascript
复制
var on: Bool = false    

@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 on == true {
        avDevice.torchMode = AVCaptureTorchMode.Off
        on = false
    } else {
        // sets the torch intensity to 100%
        do {
            let abv = try avDevice.setTorchModeOnWithLevel(1.0)
            on = true
        } catch {
            print("bbb")
        }
    //    avDevice.setTorchModeOnWithLevel(1.0, error: nil)
    }
    // unlock your device
    avDevice.unlockForConfiguration()
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33585353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档