【问题标题】:imagePickerController with GPS data (Swift)带有 GPS 数据的 imagePickerController (Swift)
【发布时间】:2014-12-03 15:12:50
【问题描述】:

如果有人可以提供帮助,我将不胜感激。我正在从https://www.youtube.com/watch?v=ztIBNHOm35Ehttps://github.com/TDAbboud/PhotosGalleryApp 学习这个非常好的快速示例代码

我正在跟踪所选相机胶卷照片中的 GPS 数据。哪个应用需要使用 imagePickerController 选择任意一张照片并放入应用相册。但是以下功能中缺少 GPS 数据(从相机胶卷中挑选照片,然后放入应用相册)。

我的问题:如何使用 imagePickerController 包含 GPS 在新相册中创建照片/图像。

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

     // http://stackoverflow.com/questions/26391158/getting-metadata-in-swift-by-uiimagepickercontroller?rq=1
    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)
  }
}

【问题讨论】:

标签: ios swift gps


【解决方案1】:

试试这个:

记得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)
                })
        }
    })
}

【讨论】:

    【解决方案2】:

    改变

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 2013-04-22
      • 1970-01-01
      • 2018-04-06
      • 2017-02-13
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多