从照片中提取GPS数据 [英] Extract GPS data from photo

查看:222
本文介绍了从照片中提取GPS数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难,因为我想从照片中提取GPS坐标。我使用函数 imagePickerController:didFinishPickingMediaWithInfo 来选择图像,我使用新的Photos框架将该图像插入到collectionView中。
我想从照片中提取GPS坐标。我做了一些研究,我知道UIImage不包含所有元数据,所以我尝试使用 AssetsLibrary 框架。
内部 didFinishPickingMediaWithInfo 我使用以下代码提取照片位置:

I have a hard time because I want to extract the GPS coordinates from a photo. I use the function imagePickerController:didFinishPickingMediaWithInfo to pick an image and the I am inserting that image in a collectionView using the new Photos framework. I want to extract the GPS coordinates from the photo. I have done some research and I am aware of the fact that UIImage does not contain all the metadata, so I tried using the AssetsLibrary framework. Inside didFinishPickingMediaWithInfo I am using the following code to extract the photo location:

    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
    }

但是,即使我检查了它也找不到位置图像和它包含EXIF元数据(它还包含我感兴趣的GPS位置)。如何从照片中检索坐标?

However, it doesn't find the location, even though I checked the image and it contains EXIF metadata (it contains also GPS locations, in which I am interested in). How can I retrieve the coordinates 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?

这篇关于从照片中提取GPS数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆