ios – Torch Freezes Extremely-Broad Digicam When Switching Between Broad & Extremely-Broad (AVFoundation Bug?)

ios – Torch Freezes Extremely-Broad Digicam When Switching Between Broad & Extremely-Broad (AVFoundation Bug?)


I am engaged on an iOS app utilizing AVFoundation to deal with actual time video seize and object detection.
Nevertheless, I’ve encountered a irritating difficulty when switching between the Broad and Extremely Broad cameras whereas the torch is ON.

Situation:

  • If the torch is ON and I change from the Broad to the Extremely Broad digital camera, the digital camera utterly freezes
  • If the Extremely Broad digital camera is energetic and I attempt to flip ON the torch, the digital camera freezes as effectively
  • The iPhone Digicam app permits utilizing the torch with Extremely Broad whereas recording video, so this needs to be doable by way of AVFoundation
  1. Is that this a recognized limitation of AVFoundation or is there a workaround to allow the torch on Extremely-Broad?
  2. How does Apple’s default Digicam app handle to allow the torch with Extremely Broad with out crashing?
  3. What’s the proper strategy to keep away from the digital camera freezing when toggling between Broad and Extremely Broad with the torch energetic?
  4. Is there a correct approach to synchronize torch activation with digital camera switching in AVFoundation?

Present Code (Torch + Digicam Change)

@IBAction func ultrawideCameraTapped(_ sender: Any?) {
    DispatchQueue.world(qos: .userInitiated).async { [weak self] in
        guard let self = self else { return }

        let isSwitchingToUltraWide = !self.isUsingFisheyeCamera
        let cameraType: AVCaptureDevice.DeviceType = isSwitchingToUltraWide ? .builtInUltraWideCamera : .builtInWideAngleCamera
        let cameraName = isSwitchingToUltraWide ? "Extremely Broad" : "Broad"

        guard let selectedCamera = AVCaptureDevice.default(cameraType, for: .video, place: .again) else {
            DispatchQueue.primary.async {
                self.showAlert(title: "Digicam Error", message: "(cameraName) digital camera is just not obtainable on this gadget.")
            }
            return
        }

        do {
            let currentInput = self.videoCapture.captureSession.inputs.first as? AVCaptureDeviceInput

            self.videoCapture.captureSession.beginConfiguration()

            // If switching to Extremely-Broad with Torch ON, try workaround
            if isSwitchingToUltraWide && self.isFlashlightOn {
                self.forceEnableTorchThroughWide()
            }

            if let currentInput = currentInput {
                self.videoCapture.captureSession.removeInput(currentInput)
            }

            let videoInput = strive AVCaptureDeviceInput(gadget: selectedCamera)
            self.videoCapture.captureSession.addInput(videoInput)

            self.videoCapture.captureSession.commitConfiguration()

            self.videoCapture.updateVideoOrientation()

            DispatchQueue.primary.async {
                if let barButton = sender as? UIBarButtonItem {
                    barButton.title = isSwitchingToUltraWide ? "Broad" : "Extremely Broad"
                    barButton.tintColor = isSwitchingToUltraWide ? UIColor.systemGreen : UIColor.white
                }
                print("Switched to (cameraName) digital camera.")
            }

            self.isUsingFisheyeCamera.toggle()
        } catch {
            DispatchQueue.primary.async {
                self.showAlert(title: "Digicam Error", message: "Failed to modify to (cameraName) digital camera: (error.localizedDescription)")
            }
        }
    }
}

func forceEnableTorchThroughWide() {
    DispatchQueue.world(qos: .userInitiated).async {
        guard let wideCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, place: .again), wideCamera.hasTorch else {
            DispatchQueue.primary.async {
                self.showAlert(title: "Torch Error", message: "Torch is just not obtainable on this gadget.")
            }
            return
        }

        do {
            strive wideCamera.lockForConfiguration()
            wideCamera.torchMode = .on
            self.isFlashlightOn = true
            wideCamera.unlockForConfiguration()
        } catch {
            DispatchQueue.primary.async {
                self.showAlert(title: "Torch Error", message: "Error whereas forcing torch via Broad Digicam: (error.localizedDescription)")
            }
        }
    }
}

rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles