2017-04-19 46 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 



     } 
    } 
} 

    }