简体   繁体   中英

iPhone 13 Pro / swift / AVFoundation builtInTripleCamera : how to enable automatic switching between cameras?

I am currently making a custom camera app for iOS with swiftUI AVFoundation. I also just bought iPhone 13 Pro. On the native Camera App, cameras automatically switch depending on FOCUS (ultrawide for macro / wideangle for intermediate distance / telephoto when focusing on far objects). This is also mentioned in documentation:

The built-in triple camera supports the following features: Automatic switching from one camera to another when zoom factor, light level, and focus position allow.

How can I make this work in my custom app? This is how I am selecting the camera (virtual device):

let session = AVCaptureDevice.DiscoverySession(deviceTypes: [ .builtInTripleCamera ], mediaType: AVMediaType.video, position: .unspecified)
let cameras = (session.devices.compactMap { $0 })
for camera in cameras {
  if camera.position == .back {
    self.rearCamera = camera
    try camera.lockForConfiguration()
    camera.exposureMode = .continuousAutoExposure
    camera.focusMode = .continuousAutoFocus
    camera.unlockForConfiguration()
  }
}

Is there a configuration I am missing?

Once you start zooming in, either by directly setting the videoZoomFactor or by calling the ramp(toVideoZoomFactor:withRate:) method for the input device, it automatically switches between the Ultrawide, Wide-angle and Telephoto lens. The switch happens based on the zoom factor being set.

A zoom factor of 1 is always the minimum. In case of the 13 Pro, setting a zoom factor of 1 will switch to the Ultrawide lens. To know which are the other zoom factors at which the system decides to switch over to the next lens, you can take a look at the virtualDeviceSwitchOverVideoZoomFactors property of the capture device. In case of the 13 Pro, it is 2 and 6 . At 2 , it will switch over to the Wide-angle lens. At 6 , it will switch over to the Telephoto lens.

You need not set any other configuration for this to work out, as long as you are using the .builtInTripleCamera device type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM