5

How can I save a photo to photo library with geolocation metadata?

I have requested (and allowed) the app to access user location:

private func allowAccessToUserLocation() {

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    }

Do I need to ask for specific premissions for the camera app?

EDIT:

I use UIImagePickerController to take photos from within the app. All photos taken from within the app are stored in the photo library WITHOUT the geolocation.

Does it have anything to do with the way I save the image to photo library? Is my problem here rather than in the core location permissions??

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
            UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

            dismiss(animated: true, completion: {

                self.showPhoto()})
        }
    }
George Asda
  • 2,119
  • 2
  • 28
  • 54
  • I don't understand what is the relation between the location and photos in photo library? can you please revise your question and fix it to be more clear. – hasan Feb 24 '17 at 12:21
  • can you please add the code where you read the geolocation data? – hasan Feb 24 '17 at 12:34
  • did you print the location before you reverseGeoCodeLocation? what is the value? – hasan Feb 24 '17 at 12:40
  • Do you use the UIImagePickerControllerReferenceURL key to get path and to then init the PHAsset? – hasan Feb 24 '17 at 15:56

2 Answers2

6

In iOS 8 Apple introduced Photo Library.

Here is the example to create and update PHAsset with location:

func addAsset(image: UIImage, location: CLLocation? = nil) {
    PHPhotoLibrary.shared().performChanges({
        // Request creating an asset from the image.
        let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
        // Set metadata location
        if let location = location {
            creationRequest.location = location
        }
    }, completionHandler: { success, error in
        if !success { NSLog("error creating asset: \(error)") }
    })
}

Make sure you authorized to access photos before saving with

PHPhotoLibrary.requestAuthorization(_:)

Metadata can be read with:

info[UIImagePickerControllerMediaMetadata]

I am not able to verify that metadata stores location, in my test NOT even with location service allowed. In that case you could get real location by yourself using CoreLocation.

XeNoN
  • 694
  • 6
  • 16
1

Well I guess you need to access the location of the user first before you call func addAsset(image: UIImage, location: CLLocation? = nil) {...}

so somewhere in the func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {....}

I am calling:

 let locationManager = CLLocationManager()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()

        }
        else{
            //Location service disabled"
        }
        //and then call....

        addAsset(image: pickedImage, location: locationManager.location)

once i finish with the:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {...}

i stop updating the location:

 locationManager.stopUpdatingLocation()

This works for me

George Asda
  • 2,119
  • 2
  • 28
  • 54