启用 AR 相机时 iPhone X 手电筒关闭 [英] iPhone X Flashlight turns off when AR Camera is enabled

查看:35
本文介绍了启用 AR 相机时 iPhone X 手电筒关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 AR 应用程序,需要将手电筒打开为手电筒模式.打开手电筒模式然后启用 AR 场景在我的 iPhone 8 上工作正常,但在 iPhone X 上,手电筒打开然后再次关闭.有没有办法解决这个问题,或者我必须做些什么才能让 iPhone X 正常工作?

I am building an AR app that needs the flashlight to be turned on torch mode. Turning on torch mode and then enabling the AR scene works fine on my iPhone 8, but on the iPhone X, it the flashlight turns on and then off again. Is there some way around this, or something specific I have to do for the iPhone X to work?

- (void)turnTorchOn:(bool) on {
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch]){

            [device lockForConfiguration:nil];
            if (on) {
                [device setTorchMode:AVCaptureTorchModeOn];
            } else {
                [device setTorchMode:AVCaptureTorchModeOff];
            }
            [device unlockForConfiguration];
        }
    }
}

然后:

self.arConfig = [ARWorldTrackingConfiguration new];
self.arConfig.planeDetection = ARPlaneDetectionHorizontal;
self.sceneView = [[ARSCNView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.sceneView];

SCNScene *scene = [SCNScene new];
self.sceneView.scene = scene;
self.sceneView.autoenablesDefaultLighting = YES;
self.sceneView.delegate = self;
self.sceneView.session.delegate = self;

更具体地说,这条线关闭了手电筒:

More specifically, this line turns off the flashlight:

self.sceneView = [[ARSCNView alloc] initWithFrame:self.view.frame];

推荐答案

我希望这段用 Swift 编写的代码略有不同(由于 guard 语句)可能适用于您的 iPhone X,但是,老实说,我还没有尝试过.

I hope this code written in Swift with a slightly different logic (due to guard statement) may work for your iPhone X but, honestly saying, I haven't tried it yet.

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
            } else {
                device.torchMode = .off
            }

            device.unlockForConfiguration()

        } catch {
            print("Error. Torch couldn't be used")
        }
    } else {
        print("Torch isn't available")
    }
}

// CALL IT:

// toggleTorch(on: true)
// toggleTorch(on: false)

希望这会有所帮助.

这篇关于启用 AR 相机时 iPhone X 手电筒关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆