diff --git a/Sources/Gallery/Camera/CameraController.swift b/Sources/Gallery/Camera/CameraController.swift index a89bda95..48825545 100644 --- a/Sources/Gallery/Camera/CameraController.swift +++ b/Sources/Gallery/Camera/CameraController.swift @@ -65,6 +65,9 @@ class CameraController: UIViewController { cameraView.stackView.addTarget(self, action: #selector(stackViewTouched(_:)), for: .touchUpInside) cameraView.shutterButton.addTarget(self, action: #selector(shutterButtonTouched(_:)), for: .touchUpInside) cameraView.doneButton.addTarget(self, action: #selector(doneButtonTouched(_:)), for: .touchUpInside) + + let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:))) + cameraView.addGestureRecognizer(pinchRecognizer) } func setupLocation() { @@ -161,6 +164,29 @@ class CameraController: UIViewController { return view } + + @objc func pinch(_ pinch : UIPinchGestureRecognizer) { + guard let device = cameraMan.currentInput?.device else { return } + let zoomFactor = device.videoZoomFactor * pinch.scale + pinch.scale = 1.0 + + let minZoomFactor = max(Config.Camera.minZoomFactor, device.minAvailableVideoZoomFactor) + let maxZoomFactor = min(Config.Camera.maxZoomFactor, device.maxAvailableVideoZoomFactor) + do { + try device.lockForConfiguration() + + defer { + device.unlockForConfiguration() + } + + if (zoomFactor <= maxZoomFactor && zoomFactor >= minZoomFactor) { + device.videoZoomFactor = zoomFactor + } + + } catch { + assertionFailure("Unable to set video zoom factor") + } + } } extension CameraController: CartDelegate { diff --git a/Sources/Gallery/Utils/Config.swift b/Sources/Gallery/Utils/Config.swift index 081f41e1..40b50b2c 100644 --- a/Sources/Gallery/Utils/Config.swift +++ b/Sources/Gallery/Utils/Config.swift @@ -54,6 +54,10 @@ public struct Config { } public static var imageLimit: Int = 0 + + public static var minZoomFactor: CGFloat = 1 + + public static var maxZoomFactor: CGFloat = 10 }