将NSDictionary添加到图像元数据

时间:2013-02-28 01:38:55

标签: ios alassetslibrary assetslibrary

我正在尝试将带有一些自定义元数据的图像保存到相册。我想保存的自定义元数据应该是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 taken 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 ***");
}

1 个答案:

答案 0 :(得分:0)

试试这个:

//covert image to nsdata format
NSData *imageData = [[NSData alloc]initWithContentsOfFile:@"your image path"];
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
[metadata setObject:imageData forKey:@"imageData"];