前置摄像头在为自定义相机转动闪光灯时会崩溃?

问题描述 投票:0回答:1

我试图在自定义相机应用程序中为相机设置闪光灯开/关,我正在尝试制作但是相机在iPhone 6中以前置相机模式打开闪光灯时仍然会崩溃。任何帮助都表示赞赏。谢谢。

if ((currentCamera?.hasFlash)!) {
    do {
        _ = try currentCamera?.lockForConfiguration()
    } catch {
        print("aaaa")
    }

    if (currentCamera?.isTorchActive)! {
        sender.setImage(UIImage(named: "flashOff"), for: .normal)
        currentCamera?.torchMode = AVCaptureDevice.TorchMode.off
    } else {
        // sets the torch intensity to 100%
        do {
            sender.setImage(UIImage(named: "flash"), for: .normal)
            _ = try currentCamera?.setTorchModeOn(level: 1.0)
        } catch {
            print("bbb")
        }
        //    avDevice.setTorchModeOnWithLevel(1.0, error: nil)
    }
    // unlock your device
    currentCamera?.unlockForConfiguration()
}
ios avfoundation swift4 avcapturedevice
1个回答
0
投票

没有破坏日志就很难回答。

//this code works for me. 

if ([self.captureDevice isFlashAvailable]) {
    if (self.captureDevice.flashActive) {
        if([self.captureDevice lockForConfiguration:nil]) {
            self.captureDevice.flashMode = AVCaptureFlashModeOff;
            [sender setTintColor:[UIColor grayColor]];
            [sender setSelected:NO];
        }
    }
    else {
        if([self.captureDevice lockForConfiguration:nil]) {
            self.captureDevice.flashMode = AVCaptureFlashModeOn;
            [sender setTintColor:[UIColor blueColor]];
            [sender setSelected:YES];
        }
    }
    [self.captureDevice unlockForConfiguration];
}
© www.soinside.com 2019 - 2024. All rights reserved.