title: How to implement "pinch to zoom" in custom camera
body: <p>I have a custom photo/video camera (think Snapchat) with a pinch recognizer to zoom in/out. Here's what's going right based on some code I found online:</p>
  
  <ul>
  <li>Zooming in somewhat works properly</li>
  <li>Capturing an image captures the zoomed image</li>
  </ul>
  
  <p>Here's what's going wrong that I need help with:</p>
  
  <ul>
  <li>Zoom out causes crash</li>
  <li>Although zooming in works, it seems to reset the zoom if I zoom in, stop touching the screen, then try to zoom in again.</li>
  <li>Capturing video resets the zoom</li>
  </ul>
  
  <p>This is my code for the pinch gesture, what should be changed?</p>
  
  <pre><code>for input in self.captureSession.inputs {
              // check that the input is a camera and not the audio
              if input.device == self.frontCameraDevice || input.device == self.backCameraDevice {
  
                  if pinch.state == UIGestureRecognizerState.Changed {
  
                      let device: AVCaptureDevice = input.device
                      let vZoomFactor = pinch.scale
                      do{
                          try device.lockForConfiguration()
                          if vZoomFactor &lt;= device.activeFormat.videoMaxZoomFactor {
                              device.videoZoomFactor = vZoomFactor
                              device.unlockForConfiguration()
                          }
                      }catch _{
                      }
                  }
  
              }
          }
  </code></pre>