带有GPS数据的imagePickerController(Swift)

7
我希望有人可以帮忙。我正在学习一个非常好的Swift示例代码,地址为https://www.youtube.com/watch?v=ztIBNHOm35Ehttps://github.com/TDAbboud/PhotosGalleryApp
我在跟踪所选相机胶卷照片的GPS数据。该应用程序需要使用imagePickerController来选择任何照片并将其放入应用程序相册中。但是在以下函数中缺少了GPS数据(从相机胶卷中选取照片然后放入应用程序相册)。
我的问题是:如何使用imagePickerController来包含GPS以创建新相册中的照片/图像。
下面是代码: Code Here
//UIImagePickerControllerDelegate Methods 
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!){

     // https://dev59.com/KITba4cB1Zd3GeqP4E-q
    let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary //Try to get gps info
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    //Implement if allowing user to edit the selected image
    //let editedImage = info.objectForKey("UIImagePickerControllerEditedImage") as UIImage

      let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
      dispatch_async(dispatch_get_global_queue(priority, 0), {
          PHPhotoLibrary.sharedPhotoLibrary().performChanges({
              let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
              let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
              let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset)!
            albumChangeRequest.addAssets([assetPlaceholder!])
            }, completionHandler: {(success, error)in
                dispatch_async(dispatch_get_main_queue(), {
                    NSLog("Adding Image to Library -> %@", (success ? "Sucess":"Error!"))
                    picker.dismissViewControllerAnimated(true, completion: nil)
                  })
          })

      })
  }
  func imagePickerControllerDidCancel(picker: UIImagePickerController!){
    picker.dismissViewControllerAnimated(true, completion: nil)
  }
}

https://dev59.com/-msz5IYBdhLWcg3wWmQy - Vitalii Gozhenko
2个回答

2

请尝试以下操作:

记得要import AssetsLibrary

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    picker.dismissViewControllerAnimated(true, completion: { () in
        if (picker.sourceType == .PhotoLibrary) {
            let image = info[UIImagePickerControllerOriginalImage] as! UIImage
            let library = ALAssetsLibrary()
            var url: NSURL = info[UIImagePickerControllerReferenceURL] as! NSURL

            library.assetForURL(url, resultBlock: { (asset: ALAsset!) in
                    if asset.valueForProperty(ALAssetPropertyLocation) != nil {
                        let latitude = (asset.valueForProperty(ALAssetPropertyLocation) as! CLLocation!).coordinate.latitude
                        let longitude = (asset.valueForProperty(ALAssetPropertyLocation) as! CLLocation!).coordinate.longitude
                        println("\(latitude), \(longitude)")
                    }
                },
                failureBlock: { (error: NSError!) in
                    println(error.localizedDescription)
                })
        }
    })
}

0

更改

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

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接