Turn on torch/flash on iPhone
the lockforConfiguration
is set in your code, where you declare your AVCaptureDevice
is a property.
[videoCaptureDevice lockForConfiguration:nil];
How to turn flashlight ON and OFF in swift?
Update #1: (torchActive
isn't returning the expected value; perhaps because it's been modified)
Update #2: For Swift 2.0
To toggle the flash from on to off (not just "on" as in mad pig's answer), you can use the following method:
func toggleFlash() {
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if (device.hasTorch) {
do {
try device.lockForConfiguration()
if (device.torchMode == AVCaptureTorchMode.On) {
device.torchMode = AVCaptureTorchMode.Off
} else {
do {
try device.setTorchModeOnWithLevel(1.0)
} catch {
print(error)
}
}
device.unlockForConfiguration()
} catch {
print(error)
}
}
}
I used nested do-catch blocks to implement Awesomeness's suggestion from the comments. This way, even if try device.setTorchModeOnWithLevel(1.0)
fails, the device is properly unlocked for configuration.
Update #3: For Swift 4:
(I edited the code a bit to my personal taste)
func toggleFlash() {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }
guard device.hasTorch else { return }
do {
try device.lockForConfiguration()
if (device.torchMode == AVCaptureDevice.TorchMode.on) {
device.torchMode = AVCaptureDevice.TorchMode.off
} else {
do {
try device.setTorchModeOn(level: 1.0)
} catch {
print(error)
}
}
device.unlockForConfiguration()
} catch {
print(error)
}
}
Original answer:
To toggle the flash from on to off (not just "on" as in mad pig's answer), you can use the following method:
func toggleFlash() {
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if (device.hasTorch) {
device.lockForConfiguration(nil)
let torchOn = !device.torchActive
device.setTorchModeOnWithLevel(1.0, error: nil)
device.torchMode = torchOn ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off
device.unlockForConfiguration()
}
}
Turn on torch/flashlight and camera at the same time on iPhone X
had the same problem. Seems to be a bug in iOS 11/iPhone X
I found a workaround ... not very elegant, but works ;-)
if let device = captureDevice {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.setTorchLevel(device: device, to: 0)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.setTorchLevel(device: device, to: 1)
}
}
Even on the iPhone the "Have you tried to turn it off and on again" works
Related Topics
Uiviewcontroller Viewdidload Vs. Viewwillappear: What Is the Proper Division of Labor
How to Create a Swift Date Object
Change Default Scrolling Behavior of Uitableview Section Header
How to Set Uiview Border Properties from Interface Builder
How Would I Tint an Image Programmatically on Ios
Basic Example For Sharing Text or Image With Uiactivityviewcontroller in Swift
Uiviewcontroller Returns Invalid Frame
Modal View Controllers - How to Display and Dismiss
Transparent iOS Navigation Bar
How to Hide the Status Bar in a Swift iOS App
Make a Phone Call Programmatically
Why Is Wkwebview Not Opening Links With Target="_Blank"
How to Build For Armv6 and Armv7 Architectures With iOS 5
Uilabel - Auto-Size Label to Fit Text
How to Launch Safari and Open Url from iOS App
Displaying a Stock iOS Notification Banner When Your App Is Open and in the Foreground
Adding a View Controller as a Subview in Another View Controller