3

我正在尝试做什么(在 10.6 下)....

我有一个图像(jpeg),其中包含图像文件中的图标(也就是说,您会看到基于文件中图像的图标,而不是程序中文件打开对话框中的通用 jpeg 图标)。我希望编辑 exif 元数据,将其保存回新文件中的图像。理想情况下,我想将其保存回文件的精确副本(即保留创建的任何自定义嵌入图标等),但是,在我手中,图标丢失了。

我的代码(为了便于阅读删除了一些位):

// 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);

我不知道这是否真的是图像处理、文件处理、保存后处理的问题(我可以使用 sip),或者我只是在思考(我怀疑是最后一个)。

缺口

4

1 回答 1

1

当您发布某些内容然后找到答案时,您不要只是讨厌它....

处理这种情况的方法是使用:

// grab the original unique icon
NSImage *theicon = [[NSWorkspace sharedWorkspace] iconForFile:full_file_path_of_original]];

// add it to the file after you have saved
BOOL done = [[NSWorkspace sharedWorkspace] setIcon:theicon forFile:full_file_path_to_new_file options:NSExcludeQuickDrawElementsIconCreationOption];

嗬!

于 2010-05-20T10:55:37.477 回答