Working with images (CGImage), exif data, and file icons

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2010-05-20T09:08:57Z Indexed on 2010/05/20 11:00 UTC
Read the original article Hit count: 375

Filed under:
|
|
|

What I am trying to do (under 10.6)....

I have an image (jpeg) that includes an icon in the image file (that is you see an icon based on the image in the file, as opposed to a generic jpeg icon in file open dialogs in a program). I wish to edit the exif metadata, save it back to the image in a new file. Ideally I would like to save this back to an exact copy of the file (i.e. preserving any custom embedded icons created etc.), however, in my hands the icon is lost.

My code (some bits removed for ease of reading):

// set up source ref I THINK THE PROBLEM IS HERE - NOT GRABBING THE INITIAL DATA
CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef) URL,NULL);

// snag metadata
NSDictionary *metadata = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL);

// make metadata mutable
NSMutableDictionary *metadataAsMutable = [[metadata mutableCopy] autorelease];

// grab exif
NSMutableDictionary *EXIFDictionary = [[[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy] autorelease];

<< edit exif >>

// add back edited exif
[metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];

// get source type
CFStringRef UTI = CGImageSourceGetType(source);

// set up write data
NSMutableData *data = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)data,UTI,1,NULL);

//add the image plus modified metadata PROBLEM HERE? NOT ADDING THE ICON
CGImageDestinationAddImageFromSource(destination,source,0, (CFDictionaryRef) metadataAsMutable);

// write to data
BOOL success = NO;
success = CGImageDestinationFinalize(destination);

// save data to disk 
[data writeToURL:saveURL atomically:YES];

//cleanup
CFRelease(destination);
CFRelease(source);

I don't know if this is really a question of image handling, file handing, post-save processing (I could use sip), or me just being think (I suspect the last).

Nick

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about images