Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport changes to focus on iOS #2426

Open
wants to merge 4 commits into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions ios/CameraView+Focus.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CameraView+focus.swift
// CameraView+Focus.swift
// mrousavy
//
// Created by Marc Rousavy on 19.02.21.
Expand All @@ -22,20 +22,61 @@ extension CameraView {

do {
try device.lockForConfiguration()
defer {
device.unlockForConfiguration()
}

// Set Focus
device.focusPointOfInterest = normalizedPoint
device.focusMode = .continuousAutoFocus
device.focusMode = .autoFocus

// Set Exposure
if device.isExposurePointOfInterestSupported {
device.exposurePointOfInterest = normalizedPoint
device.exposureMode = .continuousAutoExposure
device.exposureMode = .autoExpose
}

device.unlockForConfiguration()
// Remove any existing listeners
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.AVCaptureDeviceSubjectAreaDidChange,
object: nil)

// Listen for focus completion
device.isSubjectAreaChangeMonitoringEnabled = true
NotificationCenter.default.addObserver(self,
selector: #selector(subjectAreaDidChange),
name: NSNotification.Name.AVCaptureDeviceSubjectAreaDidChange,
object: nil)

return nil
} catch {
throw CameraError.device(DeviceError.configureError)
}
}
}

@objc
func subjectAreaDidChange(notification _: NSNotification) {
guard let device = videoDeviceInput?.device else {
return
}

try? device.lockForConfiguration()
defer {
device.unlockForConfiguration()
}

// Reset Focus to continuous/auto
if device.isFocusPointOfInterestSupported {
device.focusMode = .continuousAutoFocus
}

// Reset Exposure to continuous/auto
if device.isExposurePointOfInterestSupported {
device.exposureMode = .continuousAutoExposure
}

// Disable listeners
device.isSubjectAreaChangeMonitoringEnabled = false
}
}
Loading