AVCaptureDevice Camera Zoom

前端 未结 9 587
没有蜡笔的小新
没有蜡笔的小新 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:13

    In swift version, you can zoom in/out by simply passing scaled number on videoZoomFactor. Following code in UIPinchGestureRecognizer handler will solve the issue.

    do {
        try device.lockForConfiguration()
        switch gesture.state {
        case .began:
            self.pivotPinchScale = device.videoZoomFactor
        case .changed:
            var factor = self.pivotPinchScale * gesture.scale
            factor = max(1, min(factor, device.activeFormat.videoMaxZoomFactor))
            device.videoZoomFactor = factor
        default:
            break
        }
        device.unlockForConfiguration()
    } catch {
        // handle exception
    }
    

    In here, pivotPinchScale is a CGFloat property that declared in your controller somewhere.

    You may also refer to following project to see how camera works with UIPinchGestureRecognizer. https://github.com/DragonCherry/CameraPreviewController

提交回复
热议问题