NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Accessing Image EXIF Data (Creation Date & Location) of an image

Forums > SwiftUI

I am trying to find a reliable way of accessing the Creation Date & Location Data from an image selected from Photos. I wrote a demo app and everything seemed to work fine for some initial tests the code is as follows:

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

        let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
        image = Image(uiImage: uiImage)

        if let asset: PHAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
            print("Asset: \(asset)")
            print("Creation Data \(String(describing: asset.creationDate))")
            print("Location: \(String(describing: asset.location))")
        } else {
            print("Asset: nil")
        }
        isShown = false
    }

Originally this was correctly showing the creation data and location data but unfortunately it has now stopped working, the line:

      if let asset: PHAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {

Always returns nil

Could this be down to permissions? The info.plist file contains the following: Privacy - Photo Library Usage Description Privacy - Photo Librar Additions Usage Description

I know the image contains the relevent data because if I open the image in Photos it shows it location on a map

Is there a better way? Ideally I want to store the image in CoreData along with its creation data and location. On Andriod I would simply store the image as a .jpeg file and pull the info from the image after loading it.

3      

I'm having the same issue, did you ever find a solution @markpuk?

   

How do you do this with the new PHPhotoPicker in ios14? I need to change the creation date of images which are loaded with the PHPhotoPicker.

   

I created a blog post about it. Feel free to check it out: https://www.felixlarsen.com/blog/photo-metadata-phpickerview

   

Subscribe to Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.