【问题标题】:Turn on torch during ARSession在 ARSession 期间打开手电筒
【发布时间】:2018-12-07 19:21:11
【问题描述】:

如何在 ARSession 期间打开手电筒(手电筒)?

标准代码不起作用,只要会话开始,手电筒就关闭了。

初始化 ARSession:

        let configuration = ARWorldTrackingConfiguration()
        self.sceneView.session.run(configuration, options: [.removeExistingAnchors])

打开/关闭手电筒的功能:

private func turnTorch(enabled: Bool) {
    guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {
        return // Cant initiate avcapturedevice error
    }

    if device.hasTorch {
        do {
            try device.lockForConfiguration()

            if enabled {
                device.torchMode = .on
            } else {
                device.torchMode = .off
            }

            device.unlockForConfiguration()
        } catch {
            return //Torch could not be used, lock for configuration failed
        }

    } else {
        return // Torch not available error
    }
}

【问题讨论】:

    标签: ios swift arkit flashlight


    【解决方案1】:

    这是因为您在会话开始前的某个时间尝试打开手电筒(手电筒)。 使用 SceneKit,我尝试了这种方法并且对我来说效果很好:

    首先,将此变量添加到您的类中以了解您的手电筒(手电筒)何时打开或关闭:

    var isTorchOn = false
    

    接下来,执行ARSCNViewDelegatedidRenderScene 方法,当会话准备好时,打开那里的手电筒。由于此委托每帧执行一次,因此您只需要使用isTorchOn 来运行您的手电筒方法一次。

    另外,请注意,我已在您的 turnTorch 方法中添加了 isTorchOn 标志以更新其值。

    最终的简化代码:

    class ViewController: UIViewController, ARSCNViewDelegate {
    
        @IBOutlet var sceneView: ARSCNView!
    
        var isTorchOn = false
    
        private func turnTorch(enabled: Bool) {
            guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {
                return // Cant initiate avcapturedevice error
            }
    
            if device.hasTorch {
                do {
                    try device.lockForConfiguration()
    
                    if enabled {
                        device.torchMode = .on
                    } else {
                        device.torchMode = .off
                    }
                    self.isTorchOn = enabled // *** Add this line! ***
    
                    device.unlockForConfiguration()
                } catch {
                    return
                }
    
            } else {
                return
            }
        }
    
        func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
            if self.sceneView.session.currentFrame != nil {
                if !isTorchOn {
                    turnTorch(enabled: true)
                }
            }
        }
    
        func sessionWasInterrupted(_ session: ARSession) {
            // Probably you would want to turn the torch off here.
        }
    
        func sessionInterruptionEnded(_ session: ARSession) {
            // Probably you would want to turn the torch on again here.
        }
    
    }
    

    如果您使用 SpriteKit,您可以采用相同的方法。

    【讨论】:

    • 是的!你是对的,问题是我试图在会话开始运行之前打开它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多