title: How to set back camera zoom level to 0.5x using Swift?
body: <p>I have zoom feature working(1x onwards) for custom camera implemented using AVFoundation. This is fine till the iPhone X models. But I wanted to have 0.5x zoom in iPhone 11 and iPhone 11 Pro devices.</p>
  <p>Code that I wrote is not working to put it to 0.5x zoom. I have tried all the possible combinations of <code>[.builtInTripleCamera, .builtInDualWideCamera, .builtInUltraWideCamera]</code>. The capture device with the device type <code>.builtinUltraWideCamera</code> is not giving 0.5 for <code>minAvailableVideoZoomFactor</code>.</p>
  <p>While testing on iPhone 11, I also removed <code>[.builtInDualCamera, .builtInTelephotoCamera, .builtInWideAngleCamera, .builtInTrueDepthCamera]</code> from the <code>deviceTypes</code>.</p>
  <p>Appreciate any help to solve this. Below is the code which is working for 1x zoom onwards.</p>
  <pre><code>/// Called from -handlePinchGesture
  private func zoom(_ scale: CGFloat) {
      let captureDevice = cameraDevice(.back)
              
      do {
          try captureDevice?.lockForConfiguration()
  
          var minZoomFactor: CGFloat = captureDevice?.minAvailableVideoZoomFactor ?? 1.0
          let maxZoomFactor: CGFloat = captureDevice?.maxAvailableVideoZoomFactor ?? 1.0
          
          if #available(iOS 13.0, *) {
              if captureDevice?.deviceType == .builtInDualWideCamera || captureDevice?.deviceType == .builtInTripleCamera || captureDevice?.deviceType == .builtInUltraWideCamera {
                  minZoomFactor = 0.5
              }
          }
          zoomScale = max(minZoomFactor, min(beginZoomScale * scale, maxZoomFactor))
          captureDevice?.videoZoomFactor = zoomScale
  
          captureDevice?.unlockForConfiguration()
      } catch {
          print(&quot;ERROR: locking configuration&quot;)
      }
  }
  
  @objc private func handlePinchGesture(_ recognizer: UIPinchGestureRecognizer) {
      var allTouchesOnPreviewLayer = true
      let numTouch = recognizer.numberOfTouches
      
      for i in 0 ..&lt; numTouch {
          let location = recognizer.location(ofTouch: i, in: view)
          let convertedTouch = previewLayer.convert(location, from: previewLayer.superlayer)
          if !previewLayer.contains(convertedTouch) {
              allTouchesOnPreviewLayer = false
              break
          }
      }
      if allTouchesOnPreviewLayer {
          zoom(recognizer.scale)
      }
  }
  
  func cameraDevice(_ position: AVCaptureDevice.Position) -&gt; AVCaptureDevice? {
      var deviceTypes = [AVCaptureDevice.DeviceType]()
      deviceTypes.append(contentsOf: [.builtInDualCamera, .builtInTelephotoCamera, .builtInWideAngleCamera, .builtInTrueDepthCamera])
      
      if #available(iOS 13.0, *) {
          deviceTypes.append(contentsOf: [.builtInTripleCamera, .builtInDualWideCamera, .builtInUltraWideCamera])
      }
  
      let availableCameraDevices = AVCaptureDevice.DiscoverySession(deviceTypes: deviceTypes, mediaType: .video, position: position).devices
  
      guard availableCameraDevices.isEmpty == false else {
          debugPrint(&quot;ERROR: No camera devices found!!!&quot;)
          return nil
      }
  
      for device in availableCameraDevices {
          if device.position == position {
              return device
          }
      }
  
      guard let defaultDevice = AVCaptureDevice.default(for: AVMediaType.video) else {
          debugPrint(&quot;ERROR: Can't initialize default back camera!!!&quot;)
          return nil
      }
      return defaultDevice
  }
  </code></pre>