gpt4 book ai didi

objective-c - 在 OS X 上打开 NSImage 时从 EXIF 读取相机数据

转载 作者:行者123 更新时间:2023-12-04 04:36:32 27 4
gpt4 key购买 nike

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

最佳答案

我从一个名为 的示例项目中提取了以下代码。图像应用 由 Apple, Inc. 提供,因此您需要该项目来仔细查看下面的代码。键值绑定(bind)到 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;
}

关于objective-c - 在 OS X 上打开 NSImage 时从 EXIF 读取相机数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19665593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com