[英]Extract GPS data from photo
我很難,因為我想從照片中提取 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 中已被棄用。幸運的是,有了照片框架,這很容易實現。 當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)
}
在嘗試了很多不同的方法后終於設法得到了這個,它在蘋果文檔中的引用非常糟糕(或者我只是找不到它)。 不幸的是,任何不是通過股票“相機”應用程序拍攝的圖像往往都沒有位置元數據。 但是當他們這樣做時它工作正常。
使用iOS 11,從照片膠卷中的圖片恢復 GPS 坐標變得更加容易。
首先導入Photo 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)")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.