Skip to content

Instantly share code, notes, and snippets.

@alsedi
Created October 2, 2015 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alsedi/9d06f7bf84c567db21da to your computer and use it in GitHub Desktop.
Save alsedi/9d06f7bf84c567db21da to your computer and use it in GitHub Desktop.
// Swift 2.0 port of Objective-C code: http://www.theappguruz.com/blog/onoff-flashlight-one-button-ios
// Usage: AVCaptureDevice.turnLight()
// It does not work simultaneously with system turn of the torch
import AVFoundation
extension AVCaptureDevice {
static func turnLight() {
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if ( device.isTorchModeSupported(AVCaptureTorchMode.On) && device.isTorchModeSupported(AVCaptureTorchMode.Off)) {
do {
try device.lockForConfiguration()
} catch _ {
print("Unable lock the torch")
return
}
if device.torchMode == AVCaptureTorchMode.On {
device.torchMode = AVCaptureTorchMode.Off
} else {
device.torchMode = AVCaptureTorchMode.On
}
device.unlockForConfiguration()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment