Extract GPS data from photo

It’s hard for me because I want to extract GPS coordinates from a photo. I use the imagePickerController:didFinishPickingMediaWithInfo function to select an image, and I insert this image into the collectionView using the new Photos structure. I want to extract GPS coordinates from a photo. I did some research and I know that UIImage does not contain all the metadata, so I tried using the AssetsLibrary structure. Inside didFinishPickingMediaWithInfo To retrieve the location of the photo, I use the following code:

  var referenceURL : NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL var library : ALAssetsLibrary = ALAssetsLibrary() library.assetForURL(referenceURL, resultBlock: { (asset : ALAsset!) -> Void in var rep : ALAssetRepresentation = asset.defaultRepresentation() var metadata : NSDictionary = rep.metadata() let location: AnyObject! = asset.valueForProperty(ALAssetPropertyLocation) if location != nil { println(location) } else { println("Location not found") } }) { (error : NSError!) -> Void in } 

However, it does not find the location, although I checked the image and it contains EXIF ​​metadata (it also shows the GPS locations in which I am interested). How to get coordinates from a photo?

+6
source share
3 answers

I found a solution using the following code:

  if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary { if let currentLat = pickedLat as CLLocationDegrees? { self.latitude = pickedLat! self.longitude = pickedLong! } else { var library = ALAssetsLibrary() library.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: { (group, stop) -> Void in if (group != nil) { println("Group is not nil") println(group.valueForProperty(ALAssetsGroupPropertyName)) group.enumerateAssetsUsingBlock { (asset, index, stop) in if asset != nil { if let location: CLLocation = asset.valueForProperty(ALAssetPropertyLocation) as CLLocation! { let lat = location.coordinate.latitude let long = location.coordinate.longitude self.latitude = lat self.longitude = lat println(lat) println(long) } } } } else { println("The group is empty!") } }) { (error) -> Void in println("problem loading albums: \(error)") } } } 

What he does is that he reads the entire album and prints the location if the photo has this property, otherwise he prints “location not found”. He does this for every photo in the album. So, I have another question ... I want to display location information only for the photo I selected, and not for the entire album. Does anyone know how to do this?

+6
source

ALAssetsLibrary deprecated in iOS 10. Fortunately, using the photos framework is trivial to implement. When imagePickerController(_ picker:, didFinishPickingMediaWithInfo) , you can get location information through a simple search. Take a look at the code below:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let URL = info[UIImagePickerControllerReferenceURL] as? URL { let opts = PHFetchOptions() opts.fetchLimit = 1 let assets = PHAsset.fetchAssets(withALAssetURLs: [URL], options: opts) let asset = assets[0] // The location is "asset.location", as a CLLocation // ... Other stuff like dismiss omitted } 

Hope this helps. This is Swift 3, of course ..

+8
source
 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary { let image = info[UIImagePickerControllerOriginalImage] as! UIImage pickedImage.image = image let url = info[UIImagePickerControllerReferenceURL] as! NSURL let library = ALAssetsLibrary() library.assetForURL(url, resultBlock: { (asset) in if let location = asset.valueForProperty(ALAssetPropertyLocation) as? CLLocation { self.latitude = location.coordinate.latitude self.longitude = location.coordinate.longitude } }, failureBlock: { (error: NSError!) in print("Error!") }) } self.dismissViewControllerAnimated(true, completion: nil) } 

I finally managed to get it by trying many different ways, but this is remarkably poorly mentioned in the documentation for the apple (or I just could not find it). Unfortunately, any images that were not captured through the camera app do not have location metadata. But it works great when they do it.

Got a response from fooobar.com/questions/977771 / ...

+4
source

Source: https://habr.com/ru/post/977769/


All Articles