Reputation: 4998
In my camera app, I'm thinking of supporting tap to focus and meter. I have found, as a user, that my finger sometimes accidentally touches the screen somewhere, and it focuses there, so I want to undo it. In my app, I'm thinking of adding a Reset Focus button, which would undo the tap to focus: it would tell iOS to focus whereever it thinks is best (as it was before the tap to focus).
Does iOS offer an API for this?
When the user taps at a point, I can assign to focusPointOfInterest and exposurePointOfInterest in AVCaptureDevice. But I don't see functions clearFocusPointOfInterest() and clearExposurePointOfInterest(). How do I do this?
Upvotes: 1
Views: 255
Reputation: 178
You need to set the focus to .continousAutoFocus, the exposure to .continuousAutoExposure, and the focal point to CGPoint(x:0.5,y:0.5). The following focus and autofocus code works for me.
@IBAction private func doFocusAndExpose(_ gestureRecognizer: UITapGestureRecognizer) {
let devicePoint = previewView.videoPreviewLayer.captureDevicePointConverted(fromLayerPoint: gestureRecognizer.location(in: gestureRecognizer.view))
focus(with: .autoFocus, exposureMode: .autoExpose, at: devicePoint)
}
@IBAction private func doAutofocus(_ gestureRecognizer: UITapGestureRecognizer) {
let devicePoint = CGPoint(x:0.5,y:0.5)
focus(with: .continuousAutoFocus, exposureMode: .continuousAutoExposure, at: devicePoint)
}
private func focus(with focusMode: AVCaptureDevice.FocusMode,
exposureMode: AVCaptureDevice.ExposureMode,
at devicePoint: CGPoint) {
sessionQueue.async {
let device = self.videoDeviceInput.device
do {
try device.lockForConfiguration()
if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(focusMode) {
device.focusPointOfInterest = devicePoint
device.focusMode = focusMode
}
if device.isExposurePointOfInterestSupported && device.isExposureModeSupported(exposureMode) {
device.exposurePointOfInterest = devicePoint
device.exposureMode = exposureMode
}
device.unlockForConfiguration()
} catch {
print("Could not lock device for configuration: \(error)")
}
}
}
Upvotes: 0