现在在我的相机应用程序中,我让用户触摸任何地方来设置焦点和曝光,但是我怎样才能像苹果的相机应用程序一样获得两全其美的效果呢?
例如,用户可能希望通过触摸来关注前景中的某物,但如果场景变化足够大,它应该返回到 ContinuousAutoFocus。同样,如果用户将相机对准灯光,它应该改变曝光以使其正确显示,然后当相机返回场景时,它应该再次修复曝光,使其不会太暗。但是,他们仍然可以选择使其变亮或变暗,具体取决于他们通过相机视图触摸的内容。
现在,当视图出现时,我将默认值设置为屏幕中心:
func setDefaultFocusAndExposure() {
let focusPoint = CGPoint(x:0.5,y:0.5)
if let device = AVCaptureDevice.default(for:AVMediaType.video) {
do {
try device.lockForConfiguration()
if device.isFocusPointOfInterestSupported {
print(focusPoint)
device.focusPointOfInterest = focusPoint
device.focusMode = AVCaptureDevice.FocusMode.autoFocus
}
if device.isExposurePointOfInterestSupported {
device.exposurePointOfInterest = focusPoint
device.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
}
device.unlockForConfiguration()
} catch {
// Handle errors here
print("There was an error focusing the device's camera")
}
}
}
我还让用户根据他们触摸的位置设置焦点和曝光:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let bounds = UIScreen.main.bounds
let touchPoint = touches.first! as UITouch
let screenSize = bounds.size
let focusPoint = CGPoint(x: touchPoint.location(in: view).y / screenSize.height, y: 1.0 - touchPoint.location(in: view).x / screenSize.width)
if let device = AVCaptureDevice.default(for:AVMediaType.video) {
do {
try device.lockForConfiguration()
if device.isFocusPointOfInterestSupported {
device.focusPointOfInterest = focusPoint
device.focusMode = AVCaptureDevice.FocusMode.autoFocus
}
if device.isExposurePointOfInterestSupported {
device.exposurePointOfInterest = focusPoint
device.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
}
device.unlockForConfiguration()
} catch {
// Handle errors here
print("There was an error focusing the device's camera")
}
}
}