gpt4 book ai didi

ios - 使用前置摄像头打开灯/手电筒

转载 作者:行者123 更新时间:2023-11-28 08:08:25 28 4
gpt4 key购买 nike

我知道如何打开/关闭后置摄像头的闪光灯。我知道如何切换前置/后置摄像头。但我不知道如何独立于事件相机打开/关闭闪光灯。

我的意思是:如果事件摄像头是前置摄像头,当我打开闪光灯时,它会卡住。当我关闭它时,它会解冻。

到目前为止我的代码:

var back_cam:AVCaptureDevice?
for device in devices{
if (device as AnyObject).hasMediaType(AVMediaTypeVideo) && (device as AnyObject).position == .back{
back_cam=device as? AVCaptureDevice
}
}

guard let cam=back_cam else {
print("no back cam?")
return
}

if cam.hasTorch{
do{
try cam.lockForConfiguration()
if cam.torchMode == .on{
cam.torchMode = .off
}else{
do{
try cam.setTorchModeOnWithLevel(1)
}catch{
print(error)
}
}
cam.unlockForConfiguration()
}catch{
print(error)
}
}
}

编辑

如果我不清楚,我想保持灯亮,无论事件相机是后置还是前置。

最佳答案

在打开/关闭之前,您需要检查设备是否支持手电筒模式。

根据 Apple documentation对于

var torchMode: AVCaptureTorchMode { get set }

Before setting the value of this property, call the isTorchModeSupported(_:) method to make sure the device supports the desired mode. Setting the device to an unsupported torch mode results in the raising of an exception.

此外,当您切换到前置摄像头时,默认行为是关闭手电筒模式。将其与 iPhone/iPad 的默认相机应用进行比较。

关于ios - 使用前置摄像头打开灯/手电筒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430636/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com