I want to be able to record the data from the accelerometer and GPS while also using the camera in my app. The goal is to obtain the accelerometer and GPS data at the exact instant when the photo was taken. I am able to access the sensors and have them continuously record data, but I don't know how to get them to stop recording data at the time the image is captured. I am using Xcode 9 and Swift 4.
Asked
Active
Viewed 1,128 times
1 Answers
1
let cm = CMMotionManager()
let lm = CLLocationManager()
var motionUpdates: CMDeviceMotion?
var locationUpdates: CLLocation?
cm.accelerometerUpdateInterval = 1
cm.startDeviceMotionUpdates(to: OperationQueue.current!) { (data, error) in
motionUpdates = data
}
lm = CLLocationManager()
lm.delegate = self
lm.stopUpdatingLocation()
lm.desiredAccuracy = kCLLocationAccuracyBest
lm.startUpdatingLocation()
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
locationUpdates = manager.location
}
// --------------------------
func stopMotionUpdates() {
cm.stopDeviceMotionUpdates()
}
func stopLocationUpdates() {
lm.stopUpdatingLocation()
}
Once you capture your picture you call stopLocationUpdates()
& stopMotionUpdates()
and your most recent data will be stored in motionUpdates
and locationUpdates

Nader
- 1,120
- 1
- 9
- 22
-
Thank you for your response. I can get the acceleration updates while I have the camera open in the app but the updates don't stop when I take the image, they only stop when I click Use Photo in the bottom right hand corner. I am calling the .stopAccelerometerUpdates() from the func imagePickerController – K Pexman May 28 '18 at 21:18