ios 如何快速打开和关闭手电筒?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27207278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to turn flashlight ON and OFF in swift?
提问by Lachtan
I'd like to add flashlight functionality to my app in Swift. How can I go about doing that?
我想在 Swift 中为我的应用程序添加手电筒功能。我该怎么做呢?
回答by Lyndsey Scott
Update #1:(torchActive
isn't returning the expected value; perhaps because it's been modified)
更新#1: (torchActive
没有返回预期值,这或许是因为它已经修改)
Update #2:For Swift 2.0
更新 #2:对于 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.
我使用嵌套的 do-catch 块来实现来自评论的 Awesomeness 的建议。这样,即使try device.setTorchModeOnWithLevel(1.0)
失败,设备也能正确解锁以进行配置。
Update #3:For Swift 4:
更新 #3:对于 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()
}
}
回答by Joshua Dance
Updated Swift 4 Answer:
更新 Swift 4 答案:
func toggleTorch(on: Bool) {
guard
let device = AVCaptureDevice.default(for: AVMediaType.video),
device.hasTorch
else { return }
do {
try device.lockForConfiguration()
device.torchMode = on ? .on : .off
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
}
Then to actually turn it on or off, call the function and pass in a true or false boolean.
然后要实际打开或关闭它,请调用该函数并传入 true 或 false 布尔值。
toggleTorch(on: true)
of toggleTorch(on: false)
toggleTorch(on: true)
的 toggleTorch(on: false)
I got this answer from Hacking with Swift, however their example had an error in it.
我从Hacking with Swift得到了这个答案,但是他们的例子中有一个错误。
They used AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
but this produces an error saying defaultDevice
doesn't exist. So I changed it to AVCaptureDevice.default(for: AVMediaType.video)
他们使用了,AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
但这会产生一个错误defaultDevice
,说不存在。所以我把它改成AVCaptureDevice.default(for: AVMediaType.video)
回答by gpichler
I've updated @Lyndsey Scott's great answer for Swift 2.0
我已经更新了@Lyndsey Scott 对 Swift 2.0 的精彩回答
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if (device.hasTorch) {
do {
try device.lockForConfiguration()
if (device.torchMode == AVCaptureTorchMode.On) {
device.torchMode = AVCaptureTorchMode.Off
} else {
try device.setTorchModeOnWithLevel(1.0)
}
device.unlockForConfiguration()
} catch {
print(error)
}
}
回答by Chuy47
For swift 3
快速 3
func toggleFlash() {
if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo), device.hasTorch {
do {
try device.lockForConfiguration()
let torchOn = !device.isTorchActive
try device.setTorchModeOnWithLevel(1.0)
device.torchMode = torchOn ? .on : .off
device.unlockForConfiguration()
} catch {
print("error")
}
}
}
回答by mad pig
Like so:
像这样:
func turnTorchOn(){
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if device.hasTorch {
device.lockForConfiguration(nil)
device.setTorchModeOnWithLevel(1.0, error: nil)
device.unlockForConfiguration()
}
}
回答by Lance
For xcode 9.1, swift 4 (updated to not crash if no torch):
对于 xcode 9.1,swift 4(更新为如果没有火炬则不会崩溃):
func toggleFlash() {
let device = AVCaptureDevice.default(for: AVMediaType.video)
if (device != nil) {
if (device!.hasTorch) {
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)
}
}
}
}
回答by Alessandro Francucci
Swift 5
斯威夫特 5
The solution was already written by many, but I want to propose also the more concise one I came up in my project:
该解决方案已经由许多人编写,但我还想提出我在项目中提出的更简洁的解决方案:
func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }
guard device.hasTorch else { print("Torch isn't available"); return }
do {
try device.lockForConfiguration()
device.torchMode = on ? .on : .off
// Optional thing you may want when the torch it's on, is to manipulate the level of the torch
if on { try device.setTorchModeOn(level: AVCaptureDevice.maxAvailableTorchLevel.significand) }
device.unlockForConfiguration()
} catch {
print("Torch can't be used")
}
}
As mentioned in the comment, you can also change the torch level when it's on, which I find quite handy.
正如评论中提到的,您还可以在打开时更改手电筒级别,我觉得这非常方便。
Also import AVFoundation to use torch.
同时导入 AVFoundation 以使用火炬。
回答by Anup Gupta
Solution For Swift 4With Condition torchis available or not
Swift 4With Condition手电筒的解决方案是否可用
func flashlight() {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else{
return
}
if (device.hasTorch) {
do {
try device.lockForConfiguration()
if (device.torchMode == .on) {
device.torchMode = .off
} else {
device.torchMode = .on
}
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
print(error)
}
}
else{
print("Torch is not available")
}
}
The Solution is Combination of @Joshua DanceAnd @Lance
解决方案是@Joshua Dance和@Lance 的组合
回答by christopher saez
Swift 4.2
斯威夫特 4.2
if let device = AVCaptureDevice.default(for: AVMediaType.video) {
if (device.hasTorch) {
do {
try device.lockForConfiguration()
let torchOn = !device.isTorchActive
try device.setTorchModeOn(level: 1.0)
device.torchMode = torchOn ? AVCaptureDevice.TorchMode.on : AVCaptureDevice.TorchMode.off
device.unlockForConfiguration()
} catch {
print(error.localizedDescription)
}
}
}
回答by Mauricio Caro
Swift 4.1
@objc func Flash() {
let device = AVCaptureDevice.default(for: AVMediaType.video)
if (device?.hasTorch)! {
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)
}
}
}