1.需要导入AVFoundation模块
2.获取手机摄像头
// 1.获取摄像头
guard let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) else {return}
3.通过摄像头获取硬件的控制权
// 2.获取设备的控制权
do {
try captureDevice.lockForConfiguration()
} catch {
print(error)
return
}
4.设置手电筒的模式
/*
public enum AVCaptureTorchMode : Int {
case off
case on
case auto
}
*/
// 打开手电筒
captureDevice.torchMode = .on
// 关闭手电筒
captureDevice.torchMode = .off
5.取消硬件的控制权
// 3.释放设备的控制权
//captureDevice.unlockForConfiguration()