I want to copy native Digicam.app behaviour, particularly the macro mode.
I am utilizing digital units like .builtInTripleCamera
or .builtInDualWideCamera
and they’re able to enter macro mode with this code:
if machine.activePrimaryConstituentDeviceSwitchingBehavior != .unsupported {
machine.setPrimaryConstituentDeviceSwitchingBehavior(.auto, restrictedSwitchingBehaviorConditions: [])
}
That works nice, however now I must show in my UI that digital camera has entered macro mode. And that is the place issues get troublesome: macro mode is principally simply an ultra-wide digital camera that’s zoomed in (or cropped), proper?
We will observe when a digital machine adjustments its lively major constituent machine utilizing KVO, like this:
let activeConstituentObserver = machine.observe(.activePrimaryConstituent, choices: [.new]) { [weak self] machine, change in
guard let self = self else { return }
guard let activePrimaryConstituentDevice = change.newValue ?? machine.activePrimaryConstituent else { return }
let isMacroEnabled = activePrimaryConstituentDevice.deviceType == .builtInUltraWideCamera
DispatchQueue.most important.async {
self.cameraView.macroButton.isHidden = !isMacroEnabled
}
}
However this observer will get known as each time an lively constituent machine adjustments, i.e everytime ultra-wide digital camera turns into lively (when person zooms out to 0.5x). AVCaptureDevice
does not have “isInMacroMode” property, so what will we do? Properly, since macro mode is only a cropped ultra-wide digital camera, then we in all probability want to take a look at videoZoomFactor
property, proper? However each units have their videoZoomFactors set to their default values (0.5x and 1x from person’s perspective)
let activeConstituentObserver = machine.observe(.activePrimaryConstituent, choices: [.new]) { [weak self] machine, change in
guard let self = self else { return }
guard let activePrimaryConstituentDevice = change.newValue ?? machine.activePrimaryConstituent else { return }
let isMacroEnabled = activePrimaryConstituentDevice.deviceType == .builtInUltraWideCamera
print("activePrimaryConstituentDevice zoom: ", activePrimaryConstituentDevice.videoZoomFactor) // 1.0 (aka 0.5x)
print("virtualDevice zoom: ", machine.videoZoomFactor) // 2.0 (aka 1x)
DispatchQueue.most important.async {
self.cameraView.macroButton.isHidden = !isMacroEnabled
}
}
So my query is: how do I detect that digital machine entered macro mode (not simply modified its digital camera to ultra-wide)?