Extract GPS data from photo

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 it does is that it reads the entire album and prints the location if the photo has that property, else it prints "location not found". It does so for every photo in the album.So I have another question... I want to display the location info just for the photo that I have selected, not for the entire album. Does anyone have a clue how this can be accomplished?


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)
}

Finally managed to get this after trying a lot of different ways, it's remarkably poorly referenced in the apple documentation (or I just couldn't find it). Unfortunately any images that weren't taken through the stock "camera" app don't tend to have location metadata. But it works fine when they do.

Got the answer from https://stackoverflow.com/a/27556241/4337311


ALAssetsLibrary is deprecated in iOS 10. Fortunately, with Photos framework, this is trivial to implement. When imagePickerController(_ picker:, didFinishPickingMediaWithInfo) is called, you can retrieve location information through a simple lookup. 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..

Tags:

Ios

Swift

Ios8