0

I'm trying to save an image with some custom metadata to a Photo Album. The custom metadata I would like to save should be in the form of a NSDictionary. Until now I have only succeeded in inserting an NSArray into the image metadata by using something like below:

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

But what I would like is to pass a dictionary into setObject. Has anyone succeeded in inserting a custom metadata into an image where the metadata is an NSDIctionary?

Additional information

I'm using the code I found here Save CUSTOM metadata in an image taken from AVFoundation in iOS, to add a dictionary to the image metadata but still no luck. The original file is in the temporary directory and the final file is created via writeImageDataToSavedPhotosAlbum. Like in the link, the resulting image doesn't contain the dictionary. Any idea?

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 ***");
}
Community
  • 1
  • 1
Glauco
  • 495
  • 1
  • 7
  • 17

1 Answers1

0

Try this :

//covert image to nsdata format
NSData *imageData = [[NSData alloc]initWithContentsOfFile:@"your image path"];
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
[metadata setObject:imageData forKey:@"imageData"];
Allan
  • 2,086
  • 1
  • 18
  • 39
  • Thanks..but it didn't work. I added some additional infos to my original question. – Glauco Feb 28 '13 at 05:17
  • @Glauco what is mean didn't work ?? could you try to archived your data?? just like NSData *data = [NSKeyeArchiver archivedDataWithRootObject:"your data"]; – Allan Feb 28 '13 at 15:49
  • My idea was to add the metadata into the image. The merged image is then stored via ALAssetLibrary into a photo album. Then, at a later time, ALAssetLibrary to retrieve the images in the album. It's at this point I would like to retrieve the custom metadata stored at the beginning. I'm not sure how NSKeyeArchiver would help me. Do you have an example? or could you provide more information? – Glauco Feb 28 '13 at 18:47