ios - 从照片 iOS swift 中提取 GPS 数据

标签 ios swift gps uiimagepickercontroller latitude-longitude

我需要从 UIImagePickerController 选取的照片中获取 GPS 数据,并提取要在 UIMapKit 中使用的照片位置,我在 stackoverflow 和 Google 中尝试了一些示例代码,但没有一个对我有用!我使用的照片包含纬度和经度数据,但它总是返回 nil 并出现此 fatal error :“在展开可选值时意外发现 nil”,我在模拟器上测试该应用程序是否会导致任何问题?我考虑了各种问题,我是不是哪里做错了?这是代码

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {

    if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary {

       let url: AnyObject? = info[UIImagePickerControllerReferenceURL] as? NSURL

       let library = ALAssetsLibrary()
                    library.assetForURL(url as NSURL, resultBlock: { (asset: ALAsset!) -> Void in
           if asset != nil {
           var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
           var metaData: NSDictionary = assetRep.metadata()
           let location = metaData.objectForKey(ALAssetPropertyLocation) as CLLocation!
           let lat = location.coordinate.latitude
           let long = location.coordinate.longitude
           }
  }, failureBlock: {
       (error: NSError!) in
        NSLog("Error!")

       }

     )} dismissViewControllerAnimated(true, completion: nil)}

最佳答案

我找到了解决方案,代码是正确的!但是当我通过拖放将照片复制到模拟器时,出于某种原因从照片中删除了 GPS 数据,我将照片存储到 Dropbox 并通过模拟器的 safari 下载它们! 引用Is it possible to access a photo's geotag metadata from within the simulator?

关于ios - 从照片 iOS swift 中提取 GPS 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35089796/

相关文章:

Android:计算两个位置之间距离的最佳方法

xcode - 如何使用 SQlite Random()

ios - 带分页的 ScrollView

ios - UITextView 总是返回 nil

arrays - 将字符串转换为字符数组 swift 2.0

ios - 将字节数组转换为 PDF 文件?

iphone - iPhone 上有多个 GPS 应用程序在后台运行?

gps - 如何将 GPS 位置打印到标准输出(Ubuntu 16.04/18.04)?

ios - Swift:从枚举中的 'index' 获取 double 值

ios - Apple Watch HealthKit - 无法识别的选择器 HKHealthStore startWorkoutSession :completion: