Cocoa EXIF rotation

Dear Lazyweb,

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]);
Tags: , , , ,