3

I would like my user to use zoom in AR application. Is it possible to zoom using ARView?

I have written the following code and added it to tap action.

 let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes:
        [.builtInTrueDepthCamera, .builtInDualCamera, .builtInWideAngleCamera],
        mediaType: .video, position: .back)
    
    let devices : [AVCaptureDevice]  = discoverySession.devices
    let zoomFactor:CGFloat = 2
    
    for de in devices {
        print("name of camera")
        print(de.localizedName)
        do{
            try de.lockForConfiguration()
            de.videoZoomFactor = zoomFactor
            de.unlockForConfiguration()
        }catch {
            print ("error")
        }
               
    }

I run it on IPhone X and see result in log

name of camera
Back Dual Camera
name of camera
Back Camera

But it has no effect on zoom.

Is it even possible to zoom in while using ARKit?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

2 Answers2

1

You can't use the camera's zoom with the reality kit. Reality kit uses the camera provided by ARKit and it's fixed with a focal length of 28mm. But you can zoom on the ArView like Andy Jazz's answer.

Koreszka10
  • 37
  • 8
0

Try this approach:

// Use ARView as a subview of UIView
@IBOutlet var arView: ARView!


// Set minimumValue and default value of slider to 1
@IBAction func sliderForZooming(_ sender: UISlider) {
    
    // CGAffineTransform 3x3 matrix
    arView.transform = .init(a: CGFloat(sender.value),  b: 0,  c: 0,
                             d: CGFloat(sender.value), tx: 0, ty: 0)
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220