Search code examples
image-processingswift3exif

Error getting EXIF information in Swift 3/4


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)
}

Solution

  • 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)
                    }
                }
            }
        }