2017-04-19 48 views
0

我想從圖像選擇器中選擇的圖像中獲取地理標記位置。我使用此代碼GeoTag圖像選取器中的圖像swift 3

if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary 
{ 
    if let currentLat = pickedLat as CLLocationDegrees? 
    { 
     self.latitude = pickedLat! 
     self.longitude = pickedLong! 
    } 
    else 
    { 
    var library = ALAssetsLibrary() 
    library.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: { (group, stop) -> Void in 
      if (group != nil) { 

      println("Group is not nil") 
      println(group.valueForProperty(ALAssetsGroupPropertyName)) 
      group.enumerateAssetsUsingBlock { (asset, index, stop) in 
       if asset != nil 
       { 
       if let location: CLLocation = asset.valueForProperty(ALAssetPropertyLocation) as CLLocation! 
       { let lat = location.coordinate.latitude 
        let long = location.coordinate.longitude 

        self.latitude = lat 
        self.longitude = lat 

        println(lat) 
        println(long) 
        } 
       } 
      } 
     } else 
      { 
      println("The group is empty!") 
      } 
     }) 
     { (error) -> Void in 
      println("problem loading albums: \(error)") 
    } 
} 

}

我想知道隱蔽這些代碼迅速3。我是新與SWIFT 3。它編碼將是非常有益

回答

0

後的小時搜索我得到了我的答案

import UIKit 
    import Photos 

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
@IBOutlet weak var imageView: UIImageView! 

@IBOutlet weak var locationLabel: UILabel! 
@IBOutlet weak var timeLabel: UILabel! 


@IBOutlet weak var logi: UILabel! 

@IBOutlet weak var lati: UILabel! 





var lat = String() 
var log = String() 
var location = String() 
var timeTaken = "Not Known" 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
@IBAction func imagegeo(_ sender: Any) { 

    let imagePicker = UIImagePickerController() 
    imagePicker.sourceType = .photoLibrary 
    imagePicker.delegate = self 
    present(imagePicker, animated: true, completion: nil) 




} 


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    var chosenImage:UIImage? 



    if let URL = info[UIImagePickerControllerReferenceURL] as? URL { 
     print("We got the URL as \(URL)") 
     let opts = PHFetchOptions() 
     opts.fetchLimit = 1 
     let assets = PHAsset.fetchAssets(withALAssetURLs: [URL], options: opts) 

     print(assets) 


     for assetIndex in 0..<assets.count { 
      let asset = assets[assetIndex] 



      location = String(describing: asset.location) 



      log = String(describing: asset.location?.coordinate.longitude) 

      lat = String(describing: asset.location?.coordinate.latitude) 

      timeTaken = (asset.creationDate?.description)! 


      print(log) 


      print(location) 

      print(lat) 


     } 
    } 

    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage { 
     chosenImage = editedImage 
    } else if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     chosenImage = selectedImage 
    } 

    dismiss(animated: true) { 
     DispatchQueue.main.async { 


      self.imageView.image = chosenImage 
      self.timeLabel.text = self.timeTaken 


      self.locationLabel.text = self.location 
      self.lati.text = self.lat 
      self.logi.text = self.log 



     } 
    } 
} 

    } 
相關問題