iOS 17 swift get GPS Location from Image

166 views Asked by At

I am fetching an image from the photo library and fetch the GPS Location data, but it's not working.

This needs to work on iOS 17 as well, so I used PHPicker. kCGImagePropertyGPSDictionary is always returning nil.

The code I tried:

import UIKit
import CoreLocation
import MobileCoreServices
import PhotosUI
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var selectedImageView:UIImageView!
    @IBAction func selectTheImage() {
        self.pickImageFromLibrary_PH()
    }
    func pickImageFromLibrary_PH() {
        var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
        configuration.filter = .images
        let picker = PHPickerViewController(configuration: configuration)
        picker.delegate = self
        present(picker, animated: true, completion: nil)
    }
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        if let itemProvider = results.first?.itemProvider, itemProvider.canLoadObject(ofClass: UIImage.self) {
            itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in
                if let image = image as? UIImage {
                    self.fetchLocation(for: image)
                }
            }
        }
        picker.dismiss(animated: true, completion: nil)
    }
    func fetchLocation(for image: UIImage) {
        let locationManager = CLLocationManager()
        guard let imageData = image.jpegData(compressionQuality: 1.0) else {
            print("Unable to fetch image data.")
            return
        }
        guard let source = CGImageSourceCreateWithData(imageData as CFData, nil) else {
            print("Unable to create image source.")
            return
        }
        guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any] else {
            print("Unable to fetch image properties.")
            return
        }
        print(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("Image was taken at \(location.coordinate.latitude), \(location.coordinate.longitude)")
        } else {
            print("PHPicker- Location information not found in the image.")
        }
    }
}

Properties available in that image:

Exif/Meta-Data is available, I expect GPS location data

["ProfileName": Display P3, "{Exif}": {
    ColorSpace = 65535;
    PixelXDimension = 4032;
    PixelYDimension = 3024;
}, "DPIWidth": 72, "Depth": 8, "PixelHeight": 3024, "ColorModel": RGB, "DPIHeight": 72, "{TIFF}": {
    Orientation = 1;
    ResolutionUnit = 2;
    XResolution = 72;
    YResolution = 72;
}, "PixelWidth": 4032, "Orientation": 1, "{JFIF}": {
    DensityUnit = 0;
    JFIFVersion =     (
        1,
        0,
        1
    );
    XDensity = 72;
    YDensity = 72;
}]

In photos app location data is available, but in code, it's returning nil.

enter image description here

Note: I'm trying in Xcode 15 and iOS 17

iOS PHPickerViewController does not provide Exif/Meta-Data in this question, Exif/Meta-Data data is not available, but in our case only GPS Location data is coming as nil.

0

There are 0 answers