Ios 从照片中提取GPS数据

Ios 从照片中提取GPS数据,ios,swift,gps,uiimagepickercontroller,latitude-longitude,Ios,Swift,Gps,Uiimagepickercontroller,Latitude Longitude,我需要从UIImagePickerController拾取的照片中获取GPS数据,并提取照片的位置,以便在UIMapKit中使用。我在stackoverflow和Google中尝试了一些示例代码,但没有一个对我有效!我使用的照片包含纬度和经度数据,但它总是返回nil,并出现以下致命错误:“在展开可选值时意外发现nil”,我在emulator上测试应用程序它是否会导致任何问题?我考虑了各种各样的问题,我在哪里做错了吗?这是密码 func imagePickerController(picker:

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

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下载它们!
参考

这是一种更干净的方法

import Photos

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    guard let asset = info[.phAsset] as? PHAsset else { return }

    // Do whatever
    asset.location?.coordinate.latitude
    asset.location?.coordinate.longitude
}