Skip to content

Instantly share code, notes, and snippets.

@sumitlni
Created April 19, 2017 23:54
Show Gist options
  • Save sumitlni/6421aece205ebefa647abe701d2429e0 to your computer and use it in GitHub Desktop.
Save sumitlni/6421aece205ebefa647abe701d2429e0 to your computer and use it in GitHub Desktop.
Shows how to extract GPS data from any picture
//
// ViewController.swift
// Picture Properties
//
// Created by Sumit Chawla on 3/17/17.
// Copyright © 2017 Loud Noise Inc. All rights reserved.
//
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 geoLabel: UILabel!
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 loadNewPic(_ sender: UIBarButtonItem) {
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?
var location = "Not Known"
var timeTaken = "Not Known"
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)
for assetIndex in 0..<assets.count {
let asset = assets[assetIndex]
// print("Location: \(asset.location?.description) Taken: \(asset.creationDate)")
location = String(describing: asset.location!)
timeTaken = asset.creationDate!.description
CLGeocoder().reverseGeocodeLocation(asset.location!, completionHandler: {(placemarks, error) -> Void in
print(location)
if let error = error {
print("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks != nil && placemarks!.count > 0 {
DispatchQueue.main.async {
self.geoLabel.text = placemarks![0].locality!
}
}
else {
print("Problem with the data received from geocoder")
}
})
}
}
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.locationLabel.text = location
self.timeLabel.text = timeTaken
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment