我在应用中使用前置摄像头。我希望在拍照时用户可以放大和缩小相机
我尝试了这段代码
let device = AVCaptureDevice.default(for: .video) print(sender.scale) let vZoomFactor = sender.scale * prevZoomFactor if sender.state == .ended { prevZoomFactor = vZoomFactor >= 1 ? vZoomFactor : 1 } if sender.state == .changed{ do { try device!.lockForConfiguration() if (vZoomFactor <= device!.activeFormat.videoMaxZoomFactor) { device!.videoZoomFactor = max(1.0, min(vZoomFactor, device!.activeFormat.videoMaxZoomFactor)) device?.unlockForConfiguration() } else { print("Unable to set videoZoom: (max \(device!.activeFormat.videoMaxZoomFactor), asked \(vZoomFactor))") } } catch { print("\(error.localizedDescription)") } }
后置摄像头中的所有功能均正常运行,但前置摄像头未应用变焦。
好吧,花了几个小时在这段代码上之后,我到了我犯错的地方。
let device = AVCaptureDevice.default(for: .video)
默认情况下,这将使后置摄像头正常工作,但是当我将其切换到前置摄像头之前,直到将其视为后置摄像头,所以我只是添加了一个条件
if currentcam == frontcam { let device = frontcam //did other stuff for zooimng } else { let device = AVCaptureDevice.default(for: .video) //did other stuff for zooimng }
这对我来说很好