iOS Development

iOS 17 swift get GPS Location from Picture

Spread the love

I used to be fetching the Picture from picture library and fetch the GPS Location information, However It isn’t working

This must work on iOS 17 as nicely, So I used PHPicker kCGImagePropertyGPSDictionary is all the time returning nil.

In pictures app location information is on the market, however in code, it is returning nil.
enter image description here

The code we tried.

import UIKit
import CoreLocation
import MobileCoreServices
import PhotosUI
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var selectedImageView:UIImageView!
    @IBAction func selectTheImage() {
    func pickImageFromLibrary_PH() {
        var configuration = PHPickerConfiguration()
        configuration.filter = .photos
        let picker = PHPickerViewController(configuration: configuration)
        picker.delegate = self
        current(picker, animated: true, completion: nil)
    func picker(_ picker: PHPickerViewController, didFinishPicking outcomes: [PHPickerResult]) {
        if let itemProvider = outcomes.first?.itemProvider, itemProvider.canLoadObject(ofClass: UIImage.self) {
            itemProvider.loadObject(ofClass: UIImage.self) { (picture, error) in
                if let picture = picture as? UIImage {
                    self.fetchLocation(for: picture)
        picker.dismiss(animated: true, completion: nil)
    func fetchLocation(for picture: UIImage) {
        let locationManager = CLLocationManager()
        guard let imageData = picture.jpegData(compressionQuality: 1.0) else {
            print("Unable to fetch picture information.")
        guard let supply = CGImageSourceCreateWithData(imageData as CFData, nil) else {
            print("Unable to create picture supply.")
        guard let properties = CGImageSourceCopyPropertiesAtIndex(supply, 0, nil) as? [String: Any] else {
            print("Unable to fetch picture properties.")
        if let gpsInfo = properties[kCGImagePropertyGPSDictionary as String] as? [String: Any],
           let latitude = gpsInfo[kCGImagePropertyGPSLatitude as String] as? CLLocationDegrees,
           let longitude = gpsInfo[kCGImagePropertyGPSLongitude as String] as? CLLocationDegrees {
            let location = CLLocation(latitude: latitude, longitude: longitude)
            print("Picture was taken at (location.coordinate.latitude), (location.coordinate.longitude)")
        } else {
            print("PHPicker- Location data not discovered within the picture.")

Leave a Reply

Your email address will not be published. Required fields are marked *