2013-09-26 59 views
9

如果key/val已經存在於使用CGImageDestination的原始圖像元數據中,我似乎無法正確地將圖像元數據寫入圖像。它工作得很好,如果它的key/val不存在於原始的元數據中。你如何覆蓋圖像元數據?

就好像原始圖像中的圖像元數據屬性優先於修改。這是一種拜占庭式的格式問題,我不知道,我需要以某種不尋常的方式填充key/val,一個bug,或者?其他人看過這個?

下面的代碼和輸出,適用於正常工作(如果該值尚未設置)和寫入失敗(如果該值已設置爲其他值)的兩種情況。

任何幫助表示非常感謝。

這裏就是/ I如何創建圖像的NSData:

// convert the existing asset to nsdata to overwrite itself 
ALAssetRepresentation* rep = [asset defaultRepresentation]; 
Byte* buffer    = (Byte*)malloc(rep.size); 
NSUInteger buffered  = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil]; 
NSData* imageData   = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES]; 

// write the metadata directly into the nsdata of the image itself 
NSData* newImage = [self writeMetadataIntoImageData:imageData metadata:newMetadata]; 

這裏是所述元數據的實際的修改:

- (NSData*)writeMetadataIntoImageData:(NSData*)imageData metadata:(NSMutableDictionary*)metadataAsMutable 
{ 
    // create an imagesourceref 
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL); 

    // read and log pre write metadata 
    NSDictionary* metadata = (NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL)); 
    NSLog(@"Before:\n------------------------------%@\n------------------------------", metadata); 

    // set the new metadata keys here 
    NSMutableDictionary* iptc = [metadataAsMutable[(NSString*)kCGImagePropertyIPTCDictionary] mutableCopy]; 
    if (!iptc) 
    { 
     iptc = [NSMutableDictionary dictionaryWithCapacity:1]; 
    } 
    iptc[(NSString*)kCGImagePropertyIPTCCaptionAbstract] = @"Hardcoded Caption"; 
    metadataAsMutable[(NSString*)kCGImagePropertyIPTCDictionary] = iptc; 

    // log the new metadata as we want it written 
    NSLog(@"Parameter:\n------------------------------%@\n------------------------------", metadataAsMutable); 

    // this is the type of image (e.g., public.jpeg) 
    CFStringRef UTI = CGImageSourceGetType(source); 

    // create a new data object and write the new image into it 
    NSMutableData *dest_data = [NSMutableData data]; 
    CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL); 
    if(!destination) 
    { 
     NSLog(@"Error: Could not create image destination"); 
    } 
    // add the image contained in the image source to the destination, overidding the old metadata with our modified metadata 
    CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable); 

    BOOL success = NO; 
    success = CGImageDestinationFinalize(destination); 
    if(!success) 
    { 
     NSLog(@"Error: Could not create data from image destination"); 
    } 

    // read and log post write metadata 
    CGImageSourceRef source2; 
    source2 = CGImageSourceCreateWithData((__bridge CFDataRef) dest_data, NULL); 
    NSDictionary *metadata2 = (NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source2,0,NULL)); 
    NSLog(@"After:\n------------------------------%@\n------------------------------", metadata2); 

    // cleanup 
    CFRelease(destination); 

    // return the new data 
    return dest_data; 
} 

下面是當圖像具有現有值NSLogs關鍵:

Before: 
------------------------------{ 
    <...snip...> 
    "{IPTC}" =  { 
     "Caption/Abstract" = Blurry; 
     DateCreated = 20130923; 
     DigitalCreationDate = 20130923; 
     DigitalCreationTime = 173815; 
     Keywords =   (
      fake 
     ); 
     SupplementalCategory =   (
      fake 
     ); 
     TimeCreated = 173815; 
    }; 
    <...snip...> 
} 
------------------------------ 
Parameter: 
------------------------------{ 
    <...snip...> 
    "{IPTC}" =  { 
     "Caption/Abstract" = "Hardcoded Caption"; 
     DateCreated = 20130923; 
     DigitalCreationDate = 20130923; 
     DigitalCreationTime = 173815; 
     Keywords =   (
      fake 
     ); 
     SupplementalCategory =   (
      fake 
     ); 
     TimeCreated = 173815; 
    }; 
    <...snip...> 
} 
------------------------------ 
After: 
------------------------------{ 
    <...snip...> 
    "{IPTC}" =  { 
     "Caption/Abstract" = Blurry; 
     DateCreated = 20130923; 
     DigitalCreationDate = 20130923; 
     DigitalCreationTime = 173815; 
     Keywords =   (
      fake 
     ); 
     SupplementalCategory =   (
      fake 
     ); 
     TimeCreated = 173815; 
    }; 
    <...snip...> 
} 
------------------------------ 

這裏是NSLog的時候,圖像沒有鍵值:

Before: 
------------------------------{ 
    <...snip...> 
    "{IPTC}" =  { 
     DateCreated = 20130925; 
     DigitalCreationDate = 20130925; 
     DigitalCreationTime = 192856; 
     Keywords =   (
      fake 
     ); 
     SupplementalCategory =   (
      fake 
     ); 
     TimeCreated = 192856; 
    }; 
    <...snip...> 
} 
------------------------------ 
Parameter: 
------------------------------{ 
    <...snip...> 
    "{IPTC}" =  { 
     "Caption/Abstract" = "Hardcoded Caption"; 
     DateCreated = 20130925; 
     DigitalCreationDate = 20130925; 
     DigitalCreationTime = 192856; 
     Keywords =   (
      fake 
     ); 
     SupplementalCategory =   (
      fake 
     ); 
     TimeCreated = 192856; 
    }; 
    <...snip...> 
} 
------------------------------ 
After: 
------------------------------{ 
    <...snip...> 
    "{IPTC}" =  { 
     "Caption/Abstract" = "Hardcoded Caption"; 
     DateCreated = 20130925; 
     DigitalCreationDate = 20130925; 
     DigitalCreationTime = 192856; 
     Keywords =   (
      fake 
     ); 
     SupplementalCategory =   (
      fake 
     ); 
     TimeCreated = 192856; 
    }; 
    <...snip...> 
} 
------------------------------ 
+0

我也試過CGImageDestinationCopyImageSource,但是如果你多次編輯同一個文件,會損壞JPEG數據流。 Apple技術支持事件打開... –

+0

Hello Scott!有關這方面的消息嗎?我也有同樣的問題!請告訴我! – user2452250

+0

看看這篇文章:http://stackoverflow.com/questions/14624384/save-original-image-data-with-modified-metadata-no-re-encoding-on-ios –

回答

3

根據IPTC文檔,description字段綁定到TIFF和EXIF地址。改變TIFF中的值也會更新IPTC條目! 謝謝user2452250的提示。

+0

太棒了!可以在大約一個小時內分配50個!感謝您的幫助和時間 – user2452250

+0

對於已故的賞金感到遺憾!忘了它..在這裏它是..只是在時間..:D – user2452250

+0

不用擔心,謝謝! – mohacs