2

我正在创建一个可以打开照片、编辑和保存照片的照片应用程序。我正在使用 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 方法:

使用图像 (CGImage)、exif 数据和文件图标

但是除了文件大小之外,我无法使用该方法读取任何数据。有什么方法可以完全读取 EXIF 数据吗?

4

1 回答 1

3

我从 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;
}
于 2013-10-30T16:05:37.690 回答