GeoTag Images from image picker swift 3

筅森魡賤 提交于 2019-12-01 14:37:42

After hours of searching i got my ans

   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



        }
    }
}

   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!