gpt4 book ai didi

ios - 相机预览层添加手势缩放缩放,iOS,Swift

转载 作者:可可西里 更新时间:2023-11-01 00:56:17 24 4
gpt4 key购买 nike

我正在尝试向以编程方式添加的相机预览层添加缩放。我将下面的代码作为一个函数,但这就是我所拥有的,来自提示。我没有任何其他相关代码。我找不到关于它们的更多信息,它们似乎都专注于静止图像。

override func viewDidLoad() {
super.viewDidLoad()

let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:)))

pinchRecognizer.delegate = self
self.cameraPreviewlayer.addGestureRecognizer(pinchRecognizer)

我在这一行遇到错误

self.cameraPreviewlayer.addGestureRecognizer(pinchRecognizer)

它说 cameraPreviewLayer 没有添加 geture。

这是函数。

@objc func pinch(_ pinch: UIPinchGestureRecognizer) {

let device = videoDeviceInput.device

// Return zoom value between the minimum and maximum zoom values
func minMaxZoom(_ factor: CGFloat) -> CGFloat {
return min(min(max(factor, minimumZoom), maximumZoom), device.activeFormat.videoMaxZoomFactor)
}

func update(scale factor: CGFloat) {
do {
try device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.videoZoomFactor = factor
} catch {
print("\(error.localizedDescription)")
}
}

let newScaleFactor = minMaxZoom(pinch.scale * lastZoomFactor)

switch pinch.state {
case .began: fallthrough
case .changed: update(scale: newScaleFactor)
case .ended:
lastZoomFactor = minMaxZoom(newScaleFactor)
update(scale: lastZoomFactor)
default: break
}
}

最佳答案

我能够解决这个问题。

我所要做的就是移动

 let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:)))

pinchRecognizer.delegate = self
self.cameraPreviewlayer.addGestureRecognizer(pinchRecognizer)

从 View 加载到我设置相机预览层的位置。

    cameraPreviewlayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewlayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewlayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewlayer?.frame = self.view.frame
// scanArea.setRegionOfInterestWithProposedRegionOfInterest(regionOfInterest)
self.view.layer.insertSublayer(cameraPreviewlayer!, at: 0)
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:)))

pinchRecognizer.delegate = self
self.view.addGestureRecognizer(pinchRecognizer)

关于ios - 相机预览层添加手势缩放缩放,iOS,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47208867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com