从照片中提取GPS数据

我很困难,因为我想从照片中提取GPS坐标。 我使用函数imagePickerController:didFinishPickingMediaWithInfo来select一个图像,我使用新的Photos框架将图像插入到一个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位置),但它没有find位置。 我怎样才能从照片检索坐标?

在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 ..

我使用下面的代码find了一个解决scheme:

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

它所做的是读取整个相册并在照片具有该属性的情况下打印该位置,否则打印“未find位置”。 对于相册中的每张照片都这样做。所以我还有另外一个问题…我只想显示我select的照片的位置信息,而不是整个相册。 有没有人有一个线索如何可以完成?

 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得到答案