1

I need to get location data (coordinates for latitude and longitude) from an image picked from PhotoLibrary using UIImagePickerController. Existing answers proposes using fetchAssetsWithALAssetURLs, but it is deprecated for iOS 8 to 11, so I wonder what's the alternative?

Thanks!

just a kid
  • 153
  • 1
  • 11

2 Answers2

2

This worked for me. For new iOS versions, use the following lines of code.

var asset: PHAsset?
asset = info[UIImagePickerControllerPHAsset] as? PHAsset

Note that asset = nil if the user did not give permission to access his Photo Library since the PHAsset data is sensitive.

To obtain permission, edit Info.plist accordingly and request for permission using PHPhotoLibrary.requestAuthorization().

just a kid
  • 153
  • 1
  • 11
  • This is outdated Swift code. You had trouble with my answer since you seem to be using an older version of Swift and Xcode. – rmaddy Sep 28 '18 at 05:56
1

If you are only supporting iOS 11 and later then you can directly get the PHAsset using the .phAsset key. You only need to use PHAsset fetchAssetsWithALAssetURLs if you need to support iOS 10 or earlier.

Once you have the PHAsset reference, then you can access the location property to get the coordinates of the image.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    var asset: PHAsset?
    if #available(iOS 11.0, *) {
        asset = info[.phAsset] as? PHAsset
    } else {
        if let url = info[.referenceURL] as? URL {
            let result = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
            asset = result.firstObject
        }
    }

    if let asset = asset {
        if let location = asset.location {
            print("Image location is \(location.coordinate.latitude), \(location.coordinate.longitude)")
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Can we get it simply by `image.ciImage.properties` ? – TheTiger Sep 27 '18 at 05:37
  • @TheTiger `CIImage properties` doesn't appear to have anything to do with image metadata. It appears to be about filter metadata. – rmaddy Sep 27 '18 at 05:40
  • Have you checked `image.ciImage.properties[kCGImagePropertyGPSDictionary as String]` ? I'm not sure but this is the way I add the location to image in [this answer](https://stackoverflow.com/a/52222591/1140335). – TheTiger Sep 27 '18 at 05:45
  • @TheTiger I really don't know anything `CIImage properties`. `image.ciImage` is likely to be `nil` when selecting an image from the photo library so even if you can get the location from the `CIImage properties`, it may not work in this case anyway. Either way, the focus of the question and this answer is about accessing through the PHAsset. If you can confirm that `CIImage` works in this situation, post an answer. – rmaddy Sep 27 '18 at 05:49
  • hi thanks! I added the code accordingly and I get this error for the line " asset = info[.phAsset] as? PHAsset " : " Slice>' is not convertible to 'PHAsset' ". may i know what's wrong. sorry if it's a rookie mistake, still new to this. – just a kid Sep 27 '18 at 06:28
  • The code in my answer is written in Swift 4.2/Xcode 10. – rmaddy Sep 27 '18 at 15:50