1

I want to get EXIF data from images in my PhotoLibrary but cannot find the correct method. I have used answers from this question, and this one. I'm getting the following console output:

imageSource:  <UIImage: 0x6080000b8a20> size {3000, 2002} orientation 0 scale 1.000000
2017-09-18 11:24:28.513567+0300 PhotoTest[10581:6526935] CGImageSourceCopyPropertiesAtIndex:3517: *** ERROR: CGImageSourceCopyPropertiesAtIndex: source is not a CGImageSourceRef
2017-09-18 11:24:29.071412+0300 PhotoTest[10581:6527417] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

This is my current code attempt. I'd appreciate any solutions or ideas. I've been scratching my head trying to divine various methods, but I don't know enough about imageIO to solve the problem.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    self.dismiss(animated: true, completion: nil)

    let bigImage = info[UIImagePickerControllerOriginalImage] as? UIImage
    photoImageView.image = bigImage

     imageData = UIImagePNGRepresentation(bigImage!) as NSData?


    if let imageSource = info[UIImagePickerControllerOriginalImage] {
         print("imageSource: ", imageSource)

        let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource as! CGImageSource, 0, nil)


         //print("imageProperties: ", imageProperties!)
        if let dict = imageProperties as? [String: Any] {
            print(dict)
        }
    }

    dismiss(animated: true, completion: nil)
}
ICL1901
  • 7,632
  • 14
  • 90
  • 138

3 Answers3

1

You're seeing the:

*** ERROR: CGImageSourceCopyPropertiesAtIndex: source is not a CGImageSourceRef

line because you need to create your imageSource via one of the CGImageSourceCreate... API's.

Try doing this:

    if let bigImage = info[UIImagePickerControllerOriginalImage] as? UIImage
    {
        if let imageData = UIImagePNGRepresentation(bigImage)
        {
            if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil)
            {
                print("imageSource: ", imageSource)

                let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)

                //print("imageProperties: ", imageProperties!)
                if let dict = imageProperties as? [String: Any] {
                    print(dict)
                }
            }
        }
    }
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks a ton for this. I am getting some of what I need. However, I'm still seeing this console log: ` [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled} ` Any chance this is an iOS11 issue? – ICL1901 Sep 18 '17 at 10:38
  • That looks like an [issue with iOS 11](https://stackoverflow.com/questions/44465904/photopicker-discovery-error-error-domain-pluginkit-code-13). – Michael Dautermann Sep 18 '17 at 10:45
  • Ok .. One follow-on. I'm only seeing a list of "Chromaticities" . Is there something more I should do to get location, and camera info? Again, many thanks – ICL1901 Sep 18 '17 at 10:54
  • 1
    [Apple purposefully strips out GPS/geolocation information from EXIF data](https://stackoverflow.com/questions/10302250/reading-the-gps-data-from-the-image-returned-by-the-camera-in-ios-iphone). – Michael Dautermann Sep 18 '17 at 11:11
  • ah. ok, what about camera info? – ICL1901 Sep 18 '17 at 12:29
0

I found a different solution, as follows:

import Photos 
import PhotosUI

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

        let assetURL = info[UIImagePickerControllerReferenceURL] as! NSURL
        let asset = PHAsset.fetchAssets(withALAssetURLs: [assetURL as URL], options: nil)
        guard let result = asset.firstObject else {
            return
        }

        let imageManager = PHImageManager.default()
        imageManager.requestImageData(for: result , options: nil, resultHandler:{
            (data, responseString, imageOriet, info) -> Void in
            let imageData: NSData = data! as NSData
            if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
                let imageProperties2 = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary
                print("imageProperties2: ", imageProperties2)
            }

        })
        dismiss(animated: true, completion: nil)
    }

Hope this helps others.

ICL1901
  • 7,632
  • 14
  • 90
  • 138
0
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let exifData = info[.mediaMetadata] as? [String: Any] {
        //print(exifData)
    }
}
Abhijeet Rai
  • 119
  • 1
  • 5
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Bruno Caceiro Jan 17 '19 at 10:24
  • The question stated that the source would *not* be the camera. "This key is valid only when using an image picker whose source type is set to UIImagePickerController.SourceType.camera, and applies only to still images." https://developer.apple.com/documentation/uikit/uiimagepickercontroller/infokey/1619147-mediametadata – benc Feb 14 '21 at 01:44