Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imagePickerController with GPS data (Swift)

Tags:

ios

swift

gps

I would like to thank if anyone could help. I am learning this very good swift sample code from https://www.youtube.com/watch?v=ztIBNHOm35E or https://github.com/TDAbboud/PhotosGalleryApp

I am following the GPS data from selected Camera Roll photo. Which the App is need to use imagePickerController pick any photo and put in App Album. But the GPS data was missing from the following function (to pick photo from Camera Roll then put in the App Album).

My Question: Is how to use imagePickerController to include GPS to create photo/image in the new Album.

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)
  }
}
like image 748
PKul Avatar asked Dec 03 '14 15:12

PKul


1 Answers

Try this:

Remember to 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)
                })
        }
    })
}
like image 90
fulvio Avatar answered Nov 09 '22 01:11

fulvio