ios - 带有 GPS 数据的 imagePickerController (Swift)

标签 ios swift gps

如果有人能提供帮助,我要感谢。我正在从 https://www.youtube.com/watch?v=ztIBNHOm35E 学习这个非常好的快速示例代码或 https://github.com/TDAbboud/PhotosGalleryApp

我正在关注所选相机胶卷照片中的 GPS 数据。该App需要使用imagePickerController选取任意一张照片放入App相册。但是以下功能(从相机胶卷中选择照片然后放入应用相册)中缺少 GPS 数据。

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

Code Here

//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)
  }
}

最佳答案

试试这个:

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

关于ios - 带有 GPS 数据的 imagePickerController (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27275173/

相关文章:

android - 在我的布局中添加 mapview 的 Classnotfoundexception

android - 如何在 android 中检查 Gps 信号强度

java - NMEA校验和计算

ios - SwiftUI 中的动态转换

ios - 在 UITableView 单元格滑动中选择 'Delete',但未调用 'commit editingStyle'

ios - 使用 Swift 在 Twitter 上实现单用户 OAuth

swift - 加载更改后在 Swift 中更改 UILabel

ios - 将 NSMutableArray 添加到项目并在另一个类中访问相同的 NSMutableArray

ios - Sprite Kit 和 Swift,随着时间的推移增加节点的速度?

ios - 在 tableView 中获取 Section indexPath 的行