我正在创建一个可以打开照片,编辑和保存照片的照片应用。我正在使用GPUImage来处理我的照片,并且EXIF数据在此过程中丢失了。因此,我正在以这种方式打开文件时读取EXIF数据:
NSImage *img = [[NSImage alloc] initWithContentsOfURL:url];
NSImageRep *rep = [[img representations] objectAtIndex:0];
NSMutableDictionary *exif = [((NSDictionary*)[(id)rep valueForProperty:NSImageEXIFData]) mutableCopy];
它包含几乎所有EXIF数据,但由于某种原因它不包括Camera Maker和Camera Model。我需要在保存时保留所有数据,包括那些字段。打开图像文件时如何读取整个EXIF数据?我从这里尝试了CF方法:
Working with images (CGImage), exif data, and file icons
但是除了文件大小之外,我无法使用该方法读取任何数据。有没有方法可以完全读取EXIF数据?
答案 0 :(得分:2)
我已经从Apple,Inc。的一个名为 ImageApp 的示例项目中删除了以下代码。因此,您需要该项目仔细查看下面的代码。键值绑定到 mTree 。
// AppDelegate.h
@interface AppDelegate : NSObject {
IBOutlet NSTreeController *mTree;
NSURL *mUrl;
}
// AppDelegate.m
- (void)setPictureInfo:(NSString *)filepath {
NSURL *url = [[NSURL alloc] init];
url = [self convertpathURL:filepath:NO]; // Converting a file path to a url
[self getImageInfo:url];
}
- (void)getImageInfo:(NSURL *)url {
if (nil == url) {
return;
}
if ([url isEqual:mUrl])
return;
mUrl = url;
CGImageSourceRef source = NULL;
if (url) source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
// CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
if (source) {
// get image properties (height, width, depth, metadata etc.) for display
NSDictionary *props = (__bridge_transfer NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
[mTree setContent:[self propTree:props]];
}
else { // couldn't make image source for image, so display nothing
[mTree setContent:nil];
}
}
static NSString *ImageIOLocalizedString (NSString *key) {
static NSBundle *b = nil;
if (b == nil)
b = [NSBundle bundleWithIdentifier:@"com.apple.ImageIO.framework"];
// Returns a localized version of the string designated by 'key' in table 'CGImageSource'.
return [b localizedStringForKey:key value:key table: @"CGImageSource"];
}
- (NSArray *)propTree:(NSDictionary *)branch {
NSMutableArray *tree = [[NSMutableArray alloc] init];
for (NSInteger i3 = 0; i3 < [branch count]; i3++) {
NSArray *keys = [[branch allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSString *key = [keys objectAtIndex:i3];
NSString *locKey = ImageIOLocalizedString(key);
id obj = [branch objectForKey:key];
NSDictionary* leaf = nil;
if ([obj isKindOfClass:[NSDictionary class]])
leaf = [NSDictionary dictionaryWithObjectsAndKeys:
locKey,@"key", @"",@"val", [self propTree:obj],@"children", nil];
else
leaf = [NSDictionary dictionaryWithObjectsAndKeys:
locKey,@"key", obj,@"val", nil];
[tree addObject:leaf];
}
return tree;
}