GeoTag Images from image picker swift 3

2019-02-21 01:16发布

I want to get geotag location from image which is selected from image picker. I am using this code

   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)")
    }
}

}

i want to know to covert this code in swift 3 .I am new in coding with swift 3 .It will be very helpful

1条回答
Evening l夕情丶
2楼-- · 2019-02-21 02:02

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



        }
    }
}

   }
查看更多
登录 后发表回答