Meta data dictionary of UIImage does not got value for IPTC key

狂风中的少年 提交于 2019-12-04 20:43:38

A bit late, but here's how I did it:

Have a class (let's call it MetadataClass) with class method:

+ (void)addMetadata:(NSDictionary *)metadataToAdd toImageDataSampleBuffer:(CMSampleBufferRef)imageDataSampleBuffer
{
    // Grab metadata of image
    CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
    NSMutableDictionary *imageMetadata = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)metadataDict];
    CFRelease(metadataDict);

    for( NSString *metadataName in metadataToAdd )
    {
        id metadata = [metadataToAdd valueForKey:metadataName];

        if( [metadata isKindOfClass:NSDictionary.class] )
        {
            NSMutableDictionary *subDict = [imageMetadata valueForKey:metadataName];
            if( !subDict )
            {
                subDict = [NSMutableDictionary dictionary];
                [imageMetadata setValue:subDict forKey:metadataName];
            }
            [subDict addEntriesFromDictionary:metadata];
        }
        else if( [metadata isKindOfClass:NSString.class] )
            [imageMetadata setValue:metadata forKey:metadataName];
        else if( [metadata isKindOfClass:NSValue.class] )
            [imageMetadata setValue:metadata forKey:metadataName];
    }

    // set the dictionary back to the buffer
    CMSetAttachments(imageDataSampleBuffer, (CFMutableDictionaryRef)imageMetadata, kCMAttachmentMode_ShouldPropagate);
}

And how I use it:

    NSDictionary *iptc = @{
    (NSString *)kCGImagePropertyIPTCObjectTypeReference : @"kCGImagePropertyIPTCObjectTypeReference",
    (NSString *)kCGImagePropertyIPTCObjectAttributeReference : @"kCGImagePropertyIPTCObjectAttributeReference",
    (NSString *)kCGImagePropertyIPTCObjectName : @"kCGImagePropertyIPTCObjectName",
    (NSString *)kCGImagePropertyIPTCEditStatus : @"kCGImagePropertyIPTCEditStatus",
    (NSString *)kCGImagePropertyIPTCEditorialUpdate : @"kCGImagePropertyIPTCEditorialUpdate",
    (NSString *)kCGImagePropertyIPTCUrgency : @"kCGImagePropertyIPTCUrgency",
    (NSString *)kCGImagePropertyIPTCSubjectReference : @"kCGImagePropertyIPTCSubjectReference",
    (NSString *)kCGImagePropertyIPTCCategory : @"kCGImagePropertyIPTCCategory",
    (NSString *)kCGImagePropertyIPTCSupplementalCategory : @"kCGImagePropertyIPTCSupplementalCategory",
    (NSString *)kCGImagePropertyIPTCFixtureIdentifier : @"kCGImagePropertyIPTCFixtureIdentifier",
    (NSString *)kCGImagePropertyIPTCKeywords : @"kCGImagePropertyIPTCKeywords",
    (NSString *)kCGImagePropertyIPTCContentLocationCode : @"kCGImagePropertyIPTCContentLocationCode",
    (NSString *)kCGImagePropertyIPTCContentLocationName : @"kCGImagePropertyIPTCContentLocationName",
    (NSString *)kCGImagePropertyIPTCReleaseDate : @"kCGImagePropertyIPTCReleaseDate",
    (NSString *)kCGImagePropertyIPTCReleaseTime : @"kCGImagePropertyIPTCReleaseTime",
    (NSString *)kCGImagePropertyIPTCExpirationDate : @"kCGImagePropertyIPTCExpirationDate",
    (NSString *)kCGImagePropertyIPTCExpirationTime : @"kCGImagePropertyIPTCExpirationTime",
    (NSString *)kCGImagePropertyIPTCSpecialInstructions : @"kCGImagePropertyIPTCSpecialInstructions",
    (NSString *)kCGImagePropertyIPTCActionAdvised : @"kCGImagePropertyIPTCActionAdvised",
    (NSString *)kCGImagePropertyIPTCReferenceService : @"kCGImagePropertyIPTCReferenceService",
    (NSString *)kCGImagePropertyIPTCReferenceDate : @"kCGImagePropertyIPTCReferenceDate",
    (NSString *)kCGImagePropertyIPTCReferenceNumber : @"kCGImagePropertyIPTCReferenceNumber",
    (NSString *)kCGImagePropertyIPTCDateCreated : @"kCGImagePropertyIPTCDateCreated",
    (NSString *)kCGImagePropertyIPTCTimeCreated : @"kCGImagePropertyIPTCTimeCreated",
    (NSString *)kCGImagePropertyIPTCDigitalCreationDate : @"kCGImagePropertyIPTCDigitalCreationDate",
    (NSString *)kCGImagePropertyIPTCDigitalCreationTime : @"kCGImagePropertyIPTCDigitalCreationTime",
    (NSString *)kCGImagePropertyIPTCOriginatingProgram : <CFBundleName>,
    (NSString *)kCGImagePropertyIPTCProgramVersion : <CFBundleVersion>,
    (NSString *)kCGImagePropertyIPTCObjectCycle : @"kCGImagePropertyIPTCObjectCycle",
    (NSString *)kCGImagePropertyIPTCByline : @"kCGImagePropertyIPTCByline",
    (NSString *)kCGImagePropertyIPTCBylineTitle : @"kCGImagePropertyIPTCBylineTitle",
    (NSString *)kCGImagePropertyIPTCCity : @"kCGImagePropertyIPTCCity",
    (NSString *)kCGImagePropertyIPTCSubLocation : @"kCGImagePropertyIPTCSubLocation",
    (NSString *)kCGImagePropertyIPTCProvinceState : @"kCGImagePropertyIPTCProvinceState",
    (NSString *)kCGImagePropertyIPTCCountryPrimaryLocationCode : @"kCGImagePropertyIPTCCountryPrimaryLocationCode",
    (NSString *)kCGImagePropertyIPTCCountryPrimaryLocationName : @"kCGImagePropertyIPTCCountryPrimaryLocationName",
    (NSString *)kCGImagePropertyIPTCOriginalTransmissionReference : @"kCGImagePropertyIPTCOriginalTransmissionReference",
//    (NSString *)kCGImagePropertyIPTCHeadline: : @"kCGImagePropertyIPTCHeadline",
    (NSString *)kCGImagePropertyIPTCCredit : @"kCGImagePropertyIPTCCredit",
    (NSString *)kCGImagePropertyIPTCSource : @"kCGImagePropertyIPTCSource",
    (NSString *)kCGImagePropertyIPTCCopyrightNotice : @"kCGImagePropertyIPTCCopyrightNotice",
    (NSString *)kCGImagePropertyIPTCContact : @"kCGImagePropertyIPTCContact",
    (NSString *)kCGImagePropertyIPTCCaptionAbstract : @"kCGImagePropertyIPTCCaptionAbstract",
    (NSString *)kCGImagePropertyIPTCWriterEditor : @"kCGImagePropertyIPTCWriterEditor",
    (NSString *)kCGImagePropertyIPTCImageType : @"kCGImagePropertyIPTCImageType",
    (NSString *)kCGImagePropertyIPTCImageOrientation : @"kCGImagePropertyIPTCImageOrientation",
    (NSString *)kCGImagePropertyIPTCLanguageIdentifier : @"kCGImagePropertyIPTCLanguageIdentifier",
    (NSString *)kCGImagePropertyIPTCStarRating : @"kCGImagePropertyIPTCStarRating",
    (NSString *)kCGImagePropertyIPTCCreatorContactInfo : @"kCGImagePropertyIPTCCreatorContactInfo",
    (NSString *)kCGImagePropertyIPTCRightsUsageTerms : @"kCGImagePropertyIPTCRightsUsageTerms",
    (NSString *)kCGImagePropertyIPTCScene : @"kCGImagePropertyIPTCScene",
    (NSString *)kCGImagePropertyIPTCContactInfoCity : @"kCGImagePropertyIPTCContactInfoCity",
    (NSString *)kCGImagePropertyIPTCContactInfoCountry : @"kCGImagePropertyIPTCContactInfoCountry",
    (NSString *)kCGImagePropertyIPTCContactInfoAddress : @"kCGImagePropertyIPTCContactInfoAddress",
    (NSString *)kCGImagePropertyIPTCContactInfoPostalCode : @"kCGImagePropertyIPTCContactInfoPostalCode",
    (NSString *)kCGImagePropertyIPTCContactInfoStateProvince : @"kCGImagePropertyIPTCContactInfoStateProvince",
    (NSString *)kCGImagePropertyIPTCContactInfoEmails : @"kCGImagePropertyIPTCContactInfoEmails",
    (NSString *)kCGImagePropertyIPTCContactInfoPhones : @"kCGImagePropertyIPTCContactInfoPhones",
    (NSString *)kCGImagePropertyIPTCContactInfoWebURLs : @"kCGImagePropertyIPTCContactInfoWebURLs" };

    NSDictionary<NSString *, NSNumber *> *orientation = @{(NSString *)kCGImagePropertyOrientation : <image orientation as NSNumber> };

// Create a new mutable Dictionary and insert the IPTC sub-dictionary and the base orientation property.
    NSMutableDictionary *d = [NSMutableDictionary dictionary];
    d[(NSString *)kCGImagePropertyIPTCDictionary] = iptc;
    [d addEntriesFromDictionary:orientation];

    [MetadataClass addMetadata:d toImageDataSampleBuffer:imageDataSampleBuffer];

You can now create a NSData object containing the JPEG representation of the image, including the metadata:

NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]

And then, you can save the file using:

[jpegData writeToFile:<path of JPEG file> atomically:YES];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!