1

I have been trying for a couple of days to find a way to obtain the GPS location from a photo inside the photo library. I'm using the UIImagePicker in order to obtain the photo but no one of the solutions posted on internet seems to work. I understood I should convert the UIImage to PHAsset but everywhere people are using a deprecated method called fetchAssets(withALAssetURLs: [URL], options: PHFetchOptions?). Is there any way to achieve this? Thank you very much

Lorenzo Santini
  • 655
  • 7
  • 13

1 Answers1

1

I hope this will help you:-

//step1:- import Photos

//step2:- when you presenting imagepicker controller

    if PHPhotoLibrary.authorizationStatus() == .authorized || PHPhotoLibrary.authorizationStatus() == .authorized{
        PHPhotoLibrary.requestAuthorization { [weak self](_) in
        // Present the UIImagePickerController
        self?.present(self!.imagePicker, animated: true, completion: nil)
    }
}

swift3.0 and Swift4.0

//step3:-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    //obtaining saving path
    let fileManager = FileManager.default
    let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
    let imagePath = documentsPath?.appendingPathComponent("image.jpg")
    // extract image from the picker and save it
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let data = UIImageJPEGRepresentation(pickedImage, 0.75)
        data.write(toFile: localPath!, atomically: true)
    }

    let coordinate = (info[UIImagePickerControllerPHAsset] as? PHAsset)?.location?.coordinate
    print(coordinate?.latitude ?? "No latitude found")
    print(coordinate?.longitude ?? "No longitude found")
    self.dismiss(animated: true, completion: nil)
}

swift 4.2

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    //obtaining saving path
    let fileManager = FileManager.default
    let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
    let imagePath = documentsPath?.appendingPathComponent("image.jpg")

    // extract image from the picker and save it
    if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {

        let imageData = pickedImage.jpegData(compressionQuality: 0.75)
        try! imageData?.write(to: imagePath!)
    }

    let coordinate = (info[UIImagePickerController.InfoKey.phAsset] as? PHAsset)?.location?.coordinate
    print(coordinate?.latitude ?? "No latitude found")
    print(coordinate?.longitude ?? "No longitude found")
    self.dismiss(animated: true, completion: nil)
}

enter image description here

Thanks

V D Purohit
  • 1,179
  • 1
  • 10
  • 23
  • Sorry, but how can I use this snippet in order to find the photo's location? – Lorenzo Santini Oct 26 '18 at 14:56
  • @LorenzoSantini in code see "let imagepath" which one is path of image. – V D Purohit Oct 27 '18 at 05:37
  • Sorry, I don't want the image path, I want the gps locatiot. – Lorenzo Santini Oct 27 '18 at 09:28
  • @LorenzoSantini sorry for my bad to understand now check my updated code. – V D Purohit Oct 29 '18 at 05:24
  • 1
    I implemented your code inside my app but then it prints the "no latitude found" "no longitude found" error in my console despite inside the photo library these photos appear to have localization data. Do you know what went wrong? – Lorenzo Santini Oct 29 '18 at 12:36
  • @LorenzoSantini in my simulator it will work fine and can you please select default image provided by apple? – V D Purohit Oct 29 '18 at 12:37
  • Ok thanks...with the default image it works properly...I tried using a variance of photos from my own album and i noticed some gives me back the location while others seem to haven't this data registered at all...what determines this? – Lorenzo Santini Oct 29 '18 at 12:46
  • @LorenzoSantini it's happened due to when you captured image at that your location service for camera is off please take image with access location to camera(please look:-https://www.imore.com/how-disable-geotagging-camera-app-iphone-and-ipad) – V D Purohit Oct 29 '18 at 12:50
  • Ok, thank you a lot! I'm marking your answer as the right one. – Lorenzo Santini Oct 29 '18 at 12:51
  • @LorenzoSantini if after not working with location add comment and please check I just uploaded image in question. – V D Purohit Oct 29 '18 at 12:54
  • Yes, I saw...If will be anything wrong I'll let you know – Lorenzo Santini Oct 29 '18 at 13:24
  • Just one more question...I'm trying to package up this code inside a custom class and in order to do that I had to subclass NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate to set the imagePicker's delegate as the class itself. Now, when I try running the code it gives me back this error: "[discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}...Any help? – Lorenzo Santini Oct 29 '18 at 23:23
  • @LorenzoSantini please refer this link:-https://stackoverflow.com/questions/44465904/photopicker-discovery-error-error-domain-pluginkit-code-13 or https://discuss.multi-os-engine.org/t/uiimage-picker-controller-delegate-functions-are-never-called/1306/6 – V D Purohit Oct 30 '18 at 04:47
  • @VDPurohit you should also mention that image picker works even without `requestAuthorization` but won't get additional info like asset name – Vyachaslav Gerchicov Jun 19 '19 at 12:19