2017-03-17 17 views
1

我正在創建一個應用程序,捕獲照片並根據照片捕獲時的陀螺儀數據重新定向。在拍攝照片的確切時刻捕獲陀螺儀數據

我創建功能來捕捉與手機的陀螺儀數據:

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()方法不叫,直到用戶「確認」他們喜歡自己的搶購照片(VS奪回它)之後。

從我讀過的,有一個私人API與uiImagePickerControllerUserDidCaptureItem捕捉照片拍攝的確切時刻。我可以使用NSNotification來嘗試找到並在實際拍攝照片後調用StopUpdates()。

這是否正確,這是一個私人API?我可以用它來捕捉這一刻,否則我的應用程序會被拒絕?

同樣,有沒有更好的方式來關閉我的陀螺儀更新在拍照的確切時刻?

謝謝!

回答

0

我添加了這段代碼,它解決了這個問題,但我仍然不知道這是一個私有API,並且會被允許在應用商店中?或者如果有更好的方法來實現這一目標?

 NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "_UIImagePickerControllerUserDidCaptureItem"), object:nil, queue:nil, using: { note in 
      self.stopUpdates() 
     }) 

更新:這不會激發照片捕捉的精確時刻。當照片「鎖定」時它會閃光。按下拍照按鈕和照片鎖定到位之間有幾秒鐘的時間(2秒)。因此,如果您在此期間移動手機,則陀螺儀數據將不準確。

有關如何改善這一點的想法?