1
   import Photos

   public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage?] as? UIImage {
            newImage = image
            let asset = info[UIImagePickerController.InfoKey.phAsset] as! PHAsset
            imageCreationDate = asset.creationDate
            print (imageCreationDate)
            self.dismiss(animated: false, completion: {self.performSegue(withIdentifier: "showImageSubmit", sender: self)})
        }
    }

When a user selects an image to upload on my app I would like to record that image's create timestamp. I would like to get the image location as well but most people turn that off and it seems way more complicated, but the task at hand is getting the timestamp which must be present, if for no other reason, that images are ordered by this.

In the code I provided above I thought this was the correct way to get a PHAsset from the info of the selected image and then read the creation date. Can anyone point me to where I am going wrong.

This code fails with a

"Fatal error: Unexpectedly found nil while unwrapping an Optional value".

I know I could do an if let there to guard against that error, but more importantly why is that not getting me the asset object so I can get the create timestamp. I did some googling before getting confused and some posts suggested that maybe I need to prompt to get photo library permissions first?

Any advice would be greatly appreciated.

Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
Daniel Patriarca
  • 361
  • 3
  • 20
  • Make sure you have authorization to the `PHPhotoLibrary`, see this answer [How to retrieve PHAsset from UIImagePickerController](https://stackoverflow.com/questions/44977292/how-to-retrieve-phasset-from-uiimagepickercontroller). I was able to get the `phAsset` after including this answer. – Dennis W. Apr 15 '19 at 19:04

0 Answers0