How do you load an NSImage with proper EXIF rotation applied? I can't figure out how to determine the correct rotation. The following doesn't work. I get a dictionary containing a bunch of EXIF keys like exposure and shutter speed, but the "Orientation" tag is notably missing, and the image is displayed sideways. Preview and Finder display it properly, and exiftool says the field is there.
NSImage *img = [[NSImage alloc] initWithContentsOfFile:filename];
NSArray *reps = [img representations];
NSBitmapImageRep *rep = reps ? [reps objectAtIndex:0] : 0;
if (rep) {
NSDictionary *exif = [rep valueForProperty: @"NSImageEXIFData"];
if (exif) {
NSString *rot = [exif objectForKey:@"Orientation"];
NSLog (@"rot = %@", rot);
NSEnumerator *e = [exif keyEnumerator];
id key;
while ((key = [e nextObject])) {
NSLog (@"%@ = %@", key, [exif objectForKey:key]);
}
}
}
I have to say it's creepy that when i google "NSImageEXIFData Orientation", this is the first result.
Cocoa provides no love.
Using libexif from http://sf.net/projects/libexif :
Well that's just silly. Finder and Preview are accomplishing it somehow, and they aren't using libexif to do it.
Alright, alright.
What, were you just waiting for him to complain hard enough? We can probably take that for granted.
Thanks, I was curious about this, too.
The ImageIO framework is the way to go - http://phughes.us/node/2 has some sample code.
GraphicsImaging/Reference/CGImageProperties_Reference/Reference/reference.html has all the keys, of which kCGImagePropertyOrientation is the one you're looking for.
It looks like you want to be using ImageIO framework. Specifically a CGImageSource object, and one of the properties that you can pull out is kCGImagePropertyOrientation.
I've seen a bunch of apps get exif rotation wrong, including ones published by Flickr, so I guess it's sort of good to hear that it's not a total no-brainer to implement.