AVCaptureDevice Camera Zoom

前端 未结 9 595
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 19:51

I have a simple AVCaptureSession running to get a camera feed in my app and take photos. How can I implement the \'pinch to zoom\' functionality using a UIGestureRecognize

9条回答
  •  离开以前
    2021-01-31 20:21

    There is an easier way to handle camera zoom level with pinch recognizer. The only thing you need to do is take cameraDevice.videoZoomFactor and set it to the recognizer on .began state like this

    @objc private func viewPinched(recognizer: UIPinchGestureRecognizer) {
        switch recognizer.state {
            case .began:
                recognizer.scale = cameraDevice.videoZoomFactor
            case .changed:
                let scale = recognizer.scale
                do {
                     try cameraDevice.lockForConfiguration()
                     cameraDevice.videoZoomFactor = max(cameraDevice.minAvailableVideoZoomFactor, min(scale, cameraDevice.maxAvailableVideoZoomFactor))
                     cameraDevice.unlockForConfiguration()
                }
                catch {
                    print(error)
                }
            default:
                break
        }
    }
    

提交回复
热议问题