Created
October 2, 2015 20:18
-
-
Save alsedi/9d06f7bf84c567db21da to your computer and use it in GitHub Desktop.
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
// 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