2013-02-28 129 views
0

我試圖用一些自定義元數據將圖像保存到相冊中。我想要保存的自定義元數據應該是NSDictionary的形式。到現在爲止我已經使用類似下面只是成功地將一個NSArray到圖像元數據:將NSDictionary添加到圖像元數據

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 ***"); 
} 

回答

0

試試這個:

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

Thanks..but沒有奏效。我在原來的問題中增加了一些額外的信息。 – Glauco 2013-02-28 05:17:10

+0

@Glauco什麼意思沒有奏效?你可以嘗試存檔你的數據?就像NSData * data = [NSKeyeArchiver archivedDataWithRootObject:「your data」]; – Allan 2013-02-28 15:49:16

+0

我的想法是將元數據添加到圖像中。合併的圖像然後通過ALAssetLibrary存儲到相冊中。然後,稍後,ALAssetLibrary檢索相冊中的圖像。在這一點上,我想檢索存儲在開頭的自定義元數據。我不確定NSKeyeArchiver如何幫助我。你有一個例子嗎?還是可以提供更多信息? – Glauco 2013-02-28 18:47:30