微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 从照片中提取GPS数据

我很难,因为我想从照片中提取GPS坐标.我使用函数imagePickerController:didFinishPickingMediawithInfo来选择一个图像,我使用新的Photos框架将该图像插入到collectionView中.
我想从照片中提取GPS坐标.我做了一些研究,我知道UI Image不包含所有元数据,所以我尝试使用AssetsLibrary框架.
在didFinishPickingMediawithInfo中我使用以下代码提取照片位置:
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位置).如何从照片中检索坐标?

解决方法

在iOS 10中不推荐使用ALAssetsLibrary.幸运的是,使用Photos框架,这很容易实现.当调用imagePickerController(_ picker:,didFinishPickingMediawithInfo)时,您可以通过简单的查找来检索位置信息.看看下面的代码
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
}

希望这可以帮助.这是Swift 3,当然..

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐