swift 灯光的打开与关闭

func lightBtnAction(sender: UIButton){
        
        let device = AVCaptureDevice.default(for:.video)
        if device == nil {
            sender.isEnabled = false
            return
        }
        if device?.torchMode == .off{
            do {
                try device?.lockForConfiguration()
            } catch {
                return
            }
            device?.torchMode = .on
            device?.unlockForConfiguration()
            sender.isSelected = true
        }else {
            do {
                try device?.lockForConfiguration()
            } catch {
                return
            }
            device?.torchMode = .off
            device?.unlockForConfiguration()
            sender.isSelected = false
        }
    }

 

posted @ 2018-08-16 16:00  milaliu99  Views(713)  Comments(0)    收藏  举报