Код: Выделить всё
if device.activePrimaryConstituentDeviceSwitchingBehavior != .unsupported {
device.setPrimaryConstituentDeviceSwitchingBehavior(.auto, restrictedSwitchingBehaviorConditions: [])
}
< /code>
Это отлично работает, но теперь мне нужно отобразить в моем пользовательском интерфейсе, что камера ввела макро -режим. И вот где все становится сложным: макро-режим-это в основном просто сверхширочная камера, которая увеличивается (или обрезана), верно?let activeConstituentObserver = device.observe(\.activePrimaryConstituent, options: [.new]) { [weak self] device, change in
guard let self = self else { return }
guard let activePrimaryConstituentDevice = change.newValue ?? device.activePrimaryConstituent else { return }
let isMacroEnabled = activePrimaryConstituentDevice.deviceType == .builtInUltraWideCamera
DispatchQueue.main.async {
self.cameraView.macroButton.isHidden = !isMacroEnabled
}
}
< /code>
Но этот наблюдатель вызывается каждый раз, когда меняется активное компонентное устройство, то есть каждый раз, когда ультраизговая камера становится активной (когда пользователь увеличивается до 0,5x). AvcapturedEvice
Код: Выделить всё
let activeConstituentObserver = device.observe(\.activePrimaryConstituent, options: [.new]) { [weak self] device, change in
guard let self = self else { return }
guard let activePrimaryConstituentDevice = change.newValue ?? device.activePrimaryConstituent else { return }
let isMacroEnabled = activePrimaryConstituentDevice.deviceType == .builtInUltraWideCamera
print("activePrimaryConstituentDevice zoom: ", activePrimaryConstituentDevice.videoZoomFactor) // 1.0 (aka 0.5x)
print("virtualDevice zoom: ", device.videoZoomFactor) // 2.0 (aka 1x)
DispatchQueue.main.async {
self.cameraView.macroButton.isHidden = !isMacroEnabled
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... turedevice