2014-12-03 21 views
7

我想感謝任何人都可以提供幫助。我正在學習從https://www.youtube.com/watch?v=ztIBNHOm35Ehttps://github.com/TDAbboud/PhotosGalleryApphttps://github.com/TDAbboud/PhotosGalleryApp帶GPS數據的imagePickerController(斯威夫特)

這個非常好的快速示例代碼我正在關注所選相機膠捲照片中的GPS數據。哪些應用程序需要使用imagePickerController選擇任何照片並放入應用程序相冊。但GPS數據從以下功能中缺失(從Camera Roll中選取照片,然後放入App Album中)。

我的問題:是如何使用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) 
    } 
} 
+0

http://stackoverflow.com/questions/7965299/write-uiimage-along-with-metadata-exif-gps-tiff-in-iphones-photo-library – 2015-05-22 08:52:51

+0

難道我的回答幫助?如果是這樣,請接受它。 – fuzz 2015-08-26 05:17:03

回答

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]){