0

我正在尝试将带有一些自定义元数据的图像保存到相册中。我想保存的自定义元数据应该是 NSDictionary 的形式。到目前为止,我只成功地将 NSArray 插入到图像元数据中,方法如下:

NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
[metadata setObject:@[@"ele1", @"ele2"] forKey:(NSString*)kCGImagePropertyIPTCKeywords];
NSMutableDictionary *meta = [[NSMutableDictionary alloc] init];
[meta setObject:metadata forKey:(NSString*)kCGImagePropertyIPTCDictionary];
// pass the meta dictionary into writeImageDataToSavedPhotosAlbum

但我想要的是将字典传递给 setObject。有没有人成功地将自定义元数据插入到元数据是 NSDIctionary 的图像中?

附加信息

我正在使用我在这里找到的代码Save CUSTOM metadata in an image from AVFoundation in iOS,将字典添加到图像元数据中,但仍然没有运气。原始文件位于临时目录中,最终文件是通过writeImageDataToSavedPhotosAlbum创建的。就像在链接中一样,生成的图像不包含字典。任何想法?

CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)data, NULL);
NSDictionary *metadata = [(NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];
NSMutableDictionary *metadataAsMutable = [metadata mutableCopy];
NSMutableDictionary *RAWDictionary = [[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyRawDictionary]mutableCopy];

if(!RAWDictionary) {
    RAWDictionary = [NSMutableDictionary dictionary];
}

[RAWDictionary setValue:@"value1" forKey:@"key1"];
[RAWDictionary setValue:@"value2" forKey:@"key2"];

[metadataAsMutable setObject:RAWDictionary forKey:(NSString *)kCGImagePropertyRawDictionary];

NSMutableData *newData = [NSMutableData data];

CFStringRef UTI = CGImageSourceGetType(source); 
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)newData, UTI, 1, NULL);

if(!destination) {
    NSLog(@"***Could not create image destination ***");
}

CGImageDestinationAddImageFromSource(destination,source,0, (CFDictionaryRef)metadataAsMutable);
BOOL success = NO;
success = CGImageDestinationFinalize(destination);

if(!success) {
    NSLog(@"***Could not create data from image destination ***");
}
4

1 回答 1

0

尝试这个 :

//covert image to nsdata format
NSData *imageData = [[NSData alloc]initWithContentsOfFile:@"your image path"];
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
[metadata setObject:imageData forKey:@"imageData"];
于 2013-02-28T02:14:07.830 回答