UIImagePickerController Take Photo Not Keeping GPS Data

Swift 4.0 and iOS 11.2.1


I have an app that has the option to take a photo or grab one from the phones gallery.

If I selsct one from the gallery I can see the GPS data in the info[UIImagePickerControllerReferenceURL], but when I save it to the apps documents folder it loses the GPS data.

If I take a photo from within the app the info[UIImagePickerControllerReferenceURL] is nil?

My goal is to save the photo whether from image gallery or taken from within the app to the app document folder and have it retain the gps data.




@IBAction func takePicture(_ sender: Any)

{

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)

{


imgPicker.sourceType = .camera

imgPicker.showsCameraControls = true

self.present(imgPicker, animated: true, completion: nil)

}

}



@IBAction func getPicture(_ sender: Any)

{


imgPicker.sourceType = .photoLibrary

imgPicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!



present(imgPicker, animated: true, completion: nil)


}




func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

{

/

let image = info[UIImagePickerControllerOriginalImage] as! UIImage


let scale = 1024 / image.size.height

let newWidth = image.size.width * scale

UIGraphicsBeginImageContext(CGSize(width: newWidth, height: 1024))

image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: 1024))

let newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()




if let image = newImage

{


viewModel.images.append(RestCalls.LocalImage(lclID: 0,

pickupDeliveryID: 0,

image: image,

path: "",

uploaded: 0,

invoiceID: 0,

isSignature: 0,

isDelivery: 0,

isCustomerPickup: 0))

viewModel.createImages()

viewModel.getImages()


collectionView.reloadData()

}


//Here is where I am able to print it to the console, it just doesn't stay with the photo

if let URL = info["UIImagePickerControllerReferenceURL"] 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]

let location = String(describing: asset.location)

let log = String(describing: asset.location?.coordinate.longitude)

let lat = String(describing: asset.location?.coordinate.latitude)

let timeTaken = (asset.creationDate?.description)!

print("Longitude: \(log)")

print("Latitude: \(lat)")

print("Time: \(timeTaken)")

}

}

Did you find a solution?

I have looked at some code I wrote few years ago.


In


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

I get the URL with :


           guard let url: URL = (info as NSDictionary).object(forKey: UIImagePickerControllerReferenceURL) as? URL else { return }
UIImagePickerController Take Photo Not Keeping GPS Data
 
 
Q