ios – 使用Swift将相机焦点设置在Tap point上
发布时间:2020-12-15 01:47:56 所属栏目:百科 来源:网络整理
导读:在swift中使用相机的API似乎有些不同,我很难将相机对准一个点.当用户点击屏幕时,我希望相机对焦于该点 这是我的代码: func focusCamera(point:CGPoint) { var screenRect:CGRect = bounds var focusX = Float(point.x/screenRect.width) var focusY = Float
在swift中使用相机的API似乎有些不同,我很难将相机对准一个点.当用户点击屏幕时,我希望相机对焦于该点
这是我的代码: func focusCamera(point:CGPoint) { var screenRect:CGRect = bounds var focusX = Float(point.x/screenRect.width) var focusY = Float(point.y/screenRect.height) _currentDevice.lockForConfiguration(nil) _currentDevice.setFocusModeLockedWithLensPosition(focusX) { time in self._currentDevice.unlockForConfiguration() } _currentDevice.setFocusModeLockedWithLensPosition(focusY) { time in self._currentDevice.unlockForConfiguration() } } 但它似乎没有用. 任何建议都非常欢迎! 解决方法
来自@ryantxr for Swift 3的更新答案:
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) { let screenSize = videoView.bounds.size if let touchPoint = touches.first { let x = touchPoint.location(in: videoView).y / screenSize.height let y = 1.0 - touchPoint.location(in: videoView).x / screenSize.width let focusPoint = CGPoint(x: x,y: y) if let device = captureDevice { do { try device.lockForConfiguration() device.focusPointOfInterest = focusPoint //device.focusMode = .continuousAutoFocus device.focusMode = .autoFocus //device.focusMode = .locked device.exposurePointOfInterest = focusPoint device.exposureMode = AVCaptureExposureMode.continuousAutoExposure device.unlockForConfiguration() } catch { // just ignore } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |