捏缩放相机 [英] Pinch to zoom camera

查看:132
本文介绍了捏缩放相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试缩放相机,但我遇到了两个问题。首先,它允许用户进行太多的放大,然后进行大量的放大,其次,当我拍摄照片时,它不会采用放大视图。这是我的捏合函数代码...

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 a picture it doesn't take it of the zoomed in view. Here is my code for the pinch function...

func pinch(pinch: UIPinchGestureRecognizer) {
    if let view = cameraView {
        view.transform = CGAffineTransformScale(view.transform,
            pinch.scale, pinch.scale)
            pinch.scale = 1
    }

}

告诉我你是否需要再看代码。谢谢!

Tell me if you need to see any more code. Thanks!

推荐答案

我遇到过相机实现的相同问题。要解决这个问题,你需要了解两件事。

I have experienced the same issues with the camera implementation. To solve this you need to know about two things.


  • 最大和最小缩放必须在一个值内,否则会导致相机放大太多了。

  • 与实际图像没有保存放大图像一样,这是许多在线解决方案无法涵盖的常见错误。这实际上是因为你只是改变视图的缩放而不是实际的 AVCaptureDevice 的缩放。

  • The max and min zoom has to be within a value or else it results in the camera zooming in way too much.
  • As with the actual image not saving the zoomed in image, it is a common bug a lot of solutions online do not cover. This is actually because you are only changing the view's zoom and not the actual AVCaptureDevice's zoom.

要改变你需要的两件事:

To change the two things you need something like this:

func pinch(pinch: UIPinchGestureRecognizer) {
   var device: AVCaptureDevice = self.videoDevice
   var vZoomFactor = ((gestureRecognizer as! UIPinchGestureRecognizer).scale)
   var error:NSError!
        do{
            try device.lockForConfiguration()
            defer {device.unlockForConfiguration()}
            if (vZoomFactor <= device.activeFormat.videoMaxZoomFactor){
                device.videoZoomFactor = vZoomFactor
            }else{
            NSLog("Unable to set videoZoom: (max %f, asked %f)", device.activeFormat.videoMaxZoomFactor, vZoomFactor);
            }
        }catch error as NSError{
             NSLog("Unable to set videoZoom: %@", error.localizedDescription);
        }catch _{

        }
}

正如您所看到的,我使用视频设备的类变量( videoDevice )来跟踪我用于可视组件的捕获设备。我将缩放限制在特定范围并更改设备上的缩放属性而不是视图本身!

As you can see I use a class variable for the video device (videoDevice) to keep track of the capture device I am using for visual component. I restrict the zoom to a particular range and change the zoom property on the device and not the view itself!

这篇关于捏缩放相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆