从照片中提取GPS数据



我遇到了困难,因为我想从照片中提取GPS坐标。我使用函数imagePickerController:didFinishPickingMediaWithInfo来选择一张图像,然后使用新的照片框架将该图像插入到collectionView中。我想从照片中提取GPS坐标。我做了一些研究,我知道UIImage并不包含所有元数据,所以我尝试使用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位置)。如何从照片中检索坐标?

ALAssetsLibrary在iOS 10中已弃用。幸运的是,使用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。。

我使用以下代码找到了一个解决方案:

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

它所做的是读取整个相册并打印位置,如果照片具有该属性,则打印"找不到位置"。相册中的每张照片都是这样。所以我还有一个问题。。。我只想显示我选择的照片的位置信息,而不是整个相册的位置信息。有人知道如何做到这一点吗?

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

在尝试了很多不同的方法后,终于找到了这个,苹果文档中对它的引用非常少(或者我就是找不到它)。不幸的是,任何不是通过股票"相机"应用程序拍摄的图像往往都没有位置元数据。但当他们这样做的时候,效果很好。

答案来自https://stackoverflow.com/a/27556241/4337311

使用iOS 11,一切都变得更容易从照片卷中的图片中恢复GPS坐标。

首先导入照片SDK

import Photos

然后,在按下UIImagePickerController之前,请求访问数据的授权:

if PHPhotoLibrary.authorizationStatus() == .notDetermined {
    PHPhotoLibrary.requestAuthorization { [weak self](_) in
        // Present the UIImagePickerController
        self?.present(picker, animated: true, completion: nil)
    }
}

然后,当您从UIImagePickerController获取图像时,您只需要执行以下操作即可获取坐标:

let coordinate = (info[UIImagePickerControllerPHAsset] as? PHAsset)?.location?.coordinate

iOS 12&13

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    // For Location
    if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
        print("============> (asset.location?.coordinate.longitude ?? 0) (asset.location?.coordinate.latitude ?? 0)")
    }
}

最新更新