我在我的应用程序中使用SwiftyCam作为CustomCamera。但在相机中,我注意到焦点有时不会改变。如果我点击远处的物体它不会快速聚焦,有时我需要移动我的手机。我无法找到发生这种情况的原因。以下是用户点击屏幕时的代码段。我还附上了图片以供比较。前两个图像是从自定义相机捕获的,最后两个是Apple默认相机。
@objc fileprivate func singleTapGesture(tap: UITapGestureRecognizer) {
guard tapToFocus == true else {
// Ignore taps
return
}
let screenSize = previewLayer!.bounds.size
let tapPoint = tap.location(in: previewLayer!)
let x = tapPoint.y / screenSize.height
let y = 1.0 - tapPoint.x / screenSize.width
let focusPoint = CGPoint(x: x, y: y)
if let device = videoDevice {
do {
try device.lockForConfiguration()
if device.isFocusPointOfInterestSupported == true {
device.focusPointOfInterest = focusPoint
device.focusMode = .autoFocus
}
device.exposurePointOfInterest = focusPoint
device.exposureMode = AVCaptureDevice.ExposureMode.continuousAutoExposure
device.unlockForConfiguration()
if exposureSlider != nil {
exposureSlider.setValue(device.iso, animated: true)
}
//Call delegate function and pass in the location of the touch
DispatchQueue.main.async {
self.cameraDelegate?.swiftyCam(self, didFocusAtPoint: tapPoint)
// add it manually show the slider and change value if there is no touch then remove it after some time
if self.isexposureSliderHidden == true {
self.addSlider()
}else{
self.removeSlider()
}
}
}
catch {
// just ignore
}
}
}