How to turn flashlight ON and OFF in swift?

2019-01-16 15:56发布

I'd like to add flashlight functionality to my app in Swift. How can I go about doing that?

9条回答
欢心
2楼-- · 2019-01-16 16:33

Like so:

 func turnTorchOn(){

    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    if device.hasTorch {
        device.lockForConfiguration(nil)
        device.setTorchModeOnWithLevel(1.0, error: nil)
        device.unlockForConfiguration()
    }


}
查看更多
Luminary・发光体
3楼-- · 2019-01-16 16:33

For xcode 9.1, swift 4 (updated to not crash if no torch):

   func toggleFlash() {
    let device = AVCaptureDevice.default(for: AVMediaType.video)

    if (device != nil) {
        if (device!.hasTorch) {
            do {
                try device!.lockForConfiguration()
                    if (device!.torchMode == AVCaptureDevice.TorchMode.on) {
                        device!.torchMode = AVCaptureDevice.TorchMode.off
                    } else {
                        do {
                            try device!.setTorchModeOn(level: 1.0)
                            } catch {
                                print(error)
                            }
                    }

                    device!.unlockForConfiguration()
            } catch {
                print(error)
            }
        }
    }
}
查看更多
4楼-- · 2019-01-16 16:34

For swift 3

func toggleFlash() {
    if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo), device.hasTorch {
        do {
            try device.lockForConfiguration()
            let torchOn = !device.isTorchActive
            try device.setTorchModeOnWithLevel(1.0)
            device.torchMode = torchOn ? .on : .off
            device.unlockForConfiguration()
        } catch {
            print("error")
        }
    }
}
查看更多
Anthone
5楼-- · 2019-01-16 16:41

Updated Swift 4 Answer:

func toggleTorch(on: Bool) {
        guard let device = AVCaptureDevice.default(for: AVMediaType.video) 
        else {return}

        if device.hasTorch {
            do {
                try device.lockForConfiguration()

                if on == true {
                    device.torchMode = .on
                } else {
                    device.torchMode = .off
                }

                device.unlockForConfiguration()
            } catch {
                print("Torch could not be used")
            }
        } else {
            print("Torch is not available")
        }
    }

Then to actually turn it on or off, call the function and pass in a true or false boolean.

toggleTorch(on: true) of toggleTorch(on: false)

I got this answer from Hacking with Swift, however their example had an error in it.

They used AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) but this produces an error saying defaultDevice doesn't exist. So I changed it to AVCaptureDevice.default(for: AVMediaType.video)

查看更多
劫难
6楼-- · 2019-01-16 16:44

Swift 4.2

if let device = AVCaptureDevice.default(for: AVMediaType.video) {

    if (device.hasTorch) {
        do {
            try device.lockForConfiguration()
            let torchOn = !device.isTorchActive
            try device.setTorchModeOn(level: 1.0)
            device.torchMode = torchOn ? AVCaptureDevice.TorchMode.on : AVCaptureDevice.TorchMode.off
            device.unlockForConfiguration()
        } catch {
            print(error.localizedDescription)
        }
    }
}
查看更多
倾城 Initia
7楼-- · 2019-01-16 16:46
Swift 4.1

@objc func Flash() {
        let device = AVCaptureDevice.default(for: AVMediaType.video)
        if (device?.hasTorch)! {
            do {
                try device?.lockForConfiguration()
                if (device?.torchMode == AVCaptureDevice.TorchMode.on) {
                    device?.torchMode = AVCaptureDevice.TorchMode.off
                } else {
                    do {
                        try device?.setTorchModeOn(level: 1.0)
                    } catch {
                        print(error)
                    }
                }
                device?.unlockForConfiguration()
            } catch {
                print(error)
            }
        }
    }
查看更多
登录 后发表回答