使用图片(CGImage)、Exif数据和文件图标

3
我正在尝试做的事情(在10.6以下)是... 我有一张包含图像文件中图标的图片(也就是说,您可以在文件中看到基于图像的图标,而不是程序中打开对话框中的通用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),还是我想太多了(我怀疑是后者)。

Nick

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];

糟糕!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接