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
- Is that this a recognized limitation of AVFoundation or is there a workaround to allow the torch on Extremely-Broad?
- How does Apple’s default Digicam app handle to allow the torch with Extremely Broad with out crashing?
- What’s the proper strategy to keep away from the digital camera freezing when toggling between Broad and Extremely Broad with the torch energetic?
- 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)")
}
}
}
}