捏合手势放大和缩小相机 swift
zoom in and zoom out camera on pinch gesture swift
我在我的应用程序中使用前置摄像头。我希望用户在拍照时可以放大和缩小相机
我试过这个代码
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
}
这对我来说效果很好
我在我的应用程序中使用前置摄像头。我希望用户在拍照时可以放大和缩小相机
我试过这个代码
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
}
这对我来说效果很好