我正在创建一个应用程序,它可以拍摄照片并根据照片拍摄时的陀螺仪数据重新定位照片。
我已经创建了从手机中捕获陀螺仪数据的函数:
startUpdates()
并停止捕获数据:
stopUpdates()
我尝试将其添加到 UIImagePickerController 中:
if UIImagePickerController.isSourceTypeAvailable(.camera) {
guard ptInitials.text != "" else {
missingIdentifier(text: "Please add an identifier prior to taking a photo")
return}
startUpdates()
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraCaptureMode = .photo
imagePicker.modalPresentationStyle = .fullScreen
present(imagePicker,
animated: true,
completion: nil)
}
这会在图像捕获过程开始时启动陀螺仪捕获。
我让它将陀螺仪数据传递给一个可选的 double livePhotoAxis: Double?,同时它在“startUpdates()”函数中测量它。
我试图让它在拍摄照片后“停止”捕获数据,这样最后已知的陀螺仪数据将保存在我的变量中并能够传递给其他函数;
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
stopUpdates()
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
saveImageToDocuments(image: chosenImage, fileNameWithExtension: "image.jpg")
dismiss(animated: true, completion: nil
)
}
但是,问题是在用户“确认”他们喜欢他们拍摄的照片(与重拍照片相比)之前,不会调用 stopUpdates() 方法。
据我所知,有一个带有 uiImagePickerControllerUserDidCaptureItem 的私有(private) API 可以捕捉照片拍摄的确切时刻。我可以使用 NSNotification 来尝试找到它并在实际拍摄照片后调用 StopUpdates() 。
这是私有(private) API 是否正确?我可以用它来捕捉这一刻,还是我的应用会被拒绝?
同样,是否有更好的方法可以在拍摄照片的准确时刻关闭我的陀螺仪更新?
谢谢!
最佳答案
我添加了这段代码并解决了问题,但我仍然不知道这是否是一个私有(private) API,是否允许在应用商店中使用?或者是否有更好的方法来实现这一目标?
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "_UIImagePickerControllerUserDidCaptureItem"), object:nil, queue:nil, using: { note in
self.stopUpdates()
})
更新:这不会触发照片捕捉的确切时刻。当照片被“锁定”时它会触发。从按下拍照按钮到照片锁定到位之间有几秒钟 (2ish)。因此,如果您在此期间移动手机,陀螺仪数据将不准确。
有什么改进的想法吗?
关于ios - 在照片捕捉的准确时刻捕捉陀螺仪数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42867428/