AVCaptureSession выключение фонарика - PullRequest
0 голосов
/ 18 апреля 2019

Я пытаюсь создать новое приложение с помощью swift 5 и пытаюсь захватывать видеоизображения с включенной вспышкой. Вспышка мигает один раз, а затем выключается. Есть ли способ заставить его?

Я безуспешно пытался установить вспышку в разных местах (до и после captureSession.startRunning). Ниже представлен вид загрузки:

override func viewDidLoad() {
       super.viewDidLoad()

       configure()
       captureSession.startRunning()
       guard let captureDevice = AVCaptureDevice.default(for: .video) else {
           print("Failed to get the camera device")

           return
       }

       captureDevice.toggleTorch(on: true)
   }
        // Get the back-facing camera for capturing videos
        guard let captureDevice = AVCaptureDevice.default(for: .video) else {
            print("Failed to get the camera device")

            return
        }

        do {

            // Get an instance of the AVCaptureDeviceInput class using the previous device object.
            let input = try AVCaptureDeviceInput(device: captureDevice)


            // Set the input device on the capture session.
            captureSession.addInput(input)
            let videoDataOutput = AVCaptureVideoDataOutput()
            videoDataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "imageRecognition.queue"))
                videoDataOutput.alwaysDiscardsLateVideoFrames = true
                captureSession.addOutput(videoDataOutput)


        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }

        // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)
        // Bring the label to the front
        descriptionLabel.text = "Looking for objects..."
        view.bringSubview(toFront: descriptionLabel)
        }
extension AVCaptureDevice {
    func toggleTorch(on: Bool) {
        guard let device = AVCaptureDevice.default(for: .video) else { return }

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

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

                device.unlockForConfiguration()
            } catch {
                print("Torch could not be used")
            }
        } else {
            print("Torch is not available")
        }
    }
}

любая помощь будет принята с благодарностью!

...