我正在使用ARSCNView
来显示AR体验,这些体验通过3D SceneKit
内容增强相机视图。
管理设备相机的ARSession共享对象,但ARSession
我找不到闪光灯的任何选项。
有没有办法打开/关闭相机闪光灯?
如何使用ARKit
管理相机?
如何前后更改相机位置?
func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video)
else {return}
if device.hasTorch {
do {
try device.lockForConfiguration()
if on == true {
device.torchMode = .on // set on
} else {
device.torchMode = .off // set off
}
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}
将其称为
切换火炬(on: true) of toggleTorch(on: false)
参考: Hacking with Swift
另一个答案涵盖了手电筒,以便您的相机位置问题...
无法为 ARKit选择摄像机;也就是说,ARKit 会为您选择摄像机。
- 当您使用世界轨迹时,ARKit 始终选择背向的广角摄像头(而不是如此配备的设备上的长焦或双摄像头)。
- 当您使用面部跟踪(仅限 iPhone X)时,ARKit 始终使用前置原深感摄像头。
如果您的手电筒在您打开后立即作系统关闭,您需要做的是; 不要在启动 ARKit 会话后立即打开手电筒。一段时间后,您应该打开手电筒。我在viewDidLoad()中启动AR会话,并尝试在viewDidAppear()中打开手电筒,但操作系统随后立即关闭了它。所以,我能够让它像这样工作;
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.toggleFlash()
}
}