0

I try to retrieve exif data from a picture. I can load it in a Dictionary, but I am unable to use this Dictionary.

my Current code is :

import Cocoa
import ImageIO

let path = "/Volumes/Olivier/Original/Paysage/affoux/_OPI7684.NEF"
let UrlPath = URL(fileURLWithPath: path)

UrlPath.isFileURL
UrlPath.pathExtension
UrlPath.hasDirectoryPath


let imageSource = CGImageSourceCreateWithURL(UrlPath as CFURL, nil)
let imageProp = CGImageSourceCopyPropertiesAtIndex(imageSource!, 0, nil)
var key = "kCGImagePropertyWidth" as NSString

let h :NSDictionary = CFDictionaryGetValue(imageProp, kCGImagePropertyWidth)

The last line, doesn't work at all. Any solution ? Thank's

1 Answers1

0

The problem is that your key name is wrong. You mean kCGImagePropertyPixelWidth. And it's not a string. It's a constant. So it should not be in quotes; just use the constant directly, and don't worry what its value is.

I would suggest also that you convert to a Swift dictionary earlier in the process. Here is actual working code that you can model yourself after:

let src = CGImageSourceCreateWithURL(url as CFURL, nil)!
let result = CGImageSourceCopyPropertiesAtIndex(src, 0, nil)!
let d = result as! [AnyHashable:Any]
let width = d[kCGImagePropertyPixelWidth] as! CGFloat
let height = d[kCGImagePropertyPixelHeight] as! CGFloat

Of course that code is pretty bad because every single line contains an exclamation mark (which means "crash me"), but in real life I don't crash, so I've allowed it to stand.

matt
  • 515,959
  • 87
  • 875
  • 1,141