In my app I am scanning QR codes using camera, In case if user touches screen it should autofocus at that point but sometimes app crashes.
Here is code for that function in which app crashes at code line device.focusPointOfInterest = focusPoint
/** Touch the screen for autofocus */
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touches.first?.view == view,
let touchPoint = touches.first,
let device = videoCaptureDevice
else { return }
let videoView = view
let screenSize = videoView!.bounds.size
let xPoint = touchPoint.location(in: videoView).y / screenSize.height
let yPoint = 1.0 - touchPoint.location(in: videoView).x / screenSize.width
let focusPoint = CGPoint(x: xPoint, y: yPoint)
do {
try device.lockForConfiguration()
} catch {
return
}
// Focus to the correct point, make continiuous focus and exposure so the point stays sharp when moving the device closer
device.focusPointOfInterest = focusPoint
device.focusMode = .continuousAutoFocus
device.exposurePointOfInterest = focusPoint
device.exposureMode = AVCaptureDevice.ExposureMode.continuousAutoExposure
device.unlockForConfiguration()
}
Logs for crashes (only relevant part is attached)
Last Exception Backtrace:
0 CoreFoundation 0x180f37c60 __exceptionPreprocess + 216
(NSException.m:200)
1 libobjc.A.dylib 0x198767ee4 objc_exception_throw + 56 (objc-
exception.mm:565)
2 AVFCapture 0x1a45297f4 -[AVCaptureFigVideoDevice
setFocusPointOfInterest:] + 276 (AVCaptureFigVideoDevice.m:0)
3 QR Code Creator Scanner 0x1021f13bc specialized
CodeScannerView.ScannerViewController.touchesBegan(_:with:) + 508
(CodeScannerView.swift:307)
4 QR Code Creator Scanner 0x1021f010c
CodeScannerView.ScannerViewController.touchesBegan(_:with:) + 12 (<compiler-
generated>:0)
5 QR Code Creator Scanner 0x1021f010c @objc
CodeScannerView.ScannerViewController.touchesBegan(_:with:) + 120
6 UIKitCore 0x1833c099c forwardTouchMethod + 316
(UIResponder.m:2039)
7 UIKitCore 0x183341ed0 -[UIWindow _sendTouchesForEvent:] +
624 (UIWindow.m:2960)
The Line of code causing crash is
device.focusPointOfInterest = focusPoint
I am not ale to understand actual reason of crash.
Please check if CGPoint is valid or not or check if Device Supports Focus ?
I'm updating code check if it works in your case