我很难,因为我想从照片中提取GPS坐标.我使用该函数imagePickerController:didFinishPickingMediaWithInfo来选择图像,我使用新的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
}
Run Code Online (Sandbox Code Playgroud)
但是,它找不到位置,即使我检查了图像并且它包含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
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.这是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)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
它的作用是,如果照片具有该属性,它会读取整个相册并打印位置,否则会打印"找不到位置".对于相册中的每张照片都这样做.所以我有另一个问题......我想显示我选择的照片的位置信息,而不是整张专辑.有没有人知道如何实现这一目标?
| 归档时间: |
|
| 查看次数: |
10059 次 |
| 最近记录: |