Pinch to zoom camera

后端 未结 6 1509
再見小時候
再見小時候 2021-02-07 13:51

I\'m trying to make a pinch to zoom camera but I\'m encountering two problems. First is that it allows the user to zoom way too much in and way to much out, secondly when I take

6条回答
  •  猫巷女王i
    2021-02-07 14:41

    You can avoid saving prevZoomFactor by simply resetting UIPinchGestureRecognizer.scale to 1 like this:

        @IBAction func pinchAction(_ sender: UIPinchGestureRecognizer) {
            guard let device = currentCaptureDevice else {return}
            var zoom = device.videoZoomFactor * sender.scale
            sender.scale = 1.0
            var error:NSError!
            do{
                try device.lockForConfiguration()
                defer {device.unlockForConfiguration()}
                if zoom >= device.minAvailableVideoZoomFactor && zoom <= device.maxAvailableVideoZoomFactor {
                    device.videoZoomFactor = zoom
                }else{
                    NSLog("Unable to set videoZoom: (max %f, asked %f)", device.activeFormat.videoMaxZoomFactor, zoom);
                }
            }catch error as NSError{
                NSLog("Unable to set videoZoom: %@", error.localizedDescription);
            }catch _{
            }
        }
    

    This was recommended by Apple at a WWDC event I attended way back when gesture recognizers first came out.

提交回复
热议问题