使用图像(CGImage),exif数据和文件图标 [英] Working with images (CGImage), exif data, and file icons

查看:228
本文介绍了使用图像(CGImage),exif数据和文件图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做什么(在10.6下)....

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

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

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

推荐答案

当你发布某事,然后找到答案时,你不要讨厌。

Don't you just hate it when you post something and then find the answer....

这是使用:

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

Doh!

这篇关于使用图像(CGImage),exif数据和文件图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆