第一步:导入AVFoundation
第二步:获取到用户使用的设备
第三步:判断用户设备是否具有闪光灯,并且可用
import AVFoundation
func openTorch(){
guard let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) else {
print("无法获取到您的设备")
return
}
if device.hasTorch && device.isTorchAvailable{
try? device.lockForConfiguration()
device.torchMode = device.torchMode == .off ? .on : .off
device.unlockForConfiguration()
}
}