Last active
February 12, 2019 11:19
-
-
Save rtking1993/3e251fc1556f91dd1bc3eb006bbd416b to your computer and use it in GitHub Desktop.
Small function to use torch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setTorch(on: Bool) { | |
guard let device = AVCaptureDevice.default(for: AVMediaType.video), | |
device.hasTorch else { | |
return | |
} | |
do { | |
try device.lockForConfiguration() | |
if on { | |
do { | |
try device.setTorchModeOn(level: 1.0) | |
} catch { | |
print(error) | |
} | |
} else { | |
device.torchMode = AVCaptureDevice.TorchMode.off | |
} | |
device.unlockForConfiguration() | |
} catch { | |
print(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment