Skip to content

Instantly share code, notes, and snippets.

@1998code
Last active December 14, 2022 01:47
Show Gist options
  • Save 1998code/9e47504dc2940f154e6282191c5b5260 to your computer and use it in GitHub Desktop.
Save 1998code/9e47504dc2940f154e6282191c5b5260 to your computer and use it in GitHub Desktop.
Torch Light Function
import SwiftUI
import AVFoundation
func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: .video) else { return }
if device.hasTorch {
do {
try device.lockForConfiguration()
device.torchMode = on ? .on : .off
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment