UIImage的元数据字典没有IPTC密钥的值 [英] Meta data dictionary of UIImage does not got value for IPTC key

查看:100
本文介绍了UIImage的元数据字典没有IPTC密钥的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在保存屏幕快照图像时,我试图更改UIImage对象的元数据以用于图像的自定义标题和描述.

While saving screenshot image I am trying to change meta data of UIImage object for custom title and description of image.

导入的标题

#import <AssetsLibrary/AssetsLibrary.h>
#import <ImageIO/ImageIO.h>
#import <ImageIO/CGImageProperties.h>

在我的方法之内

  NSData* dataOfImageFromGallery = UIImageJPEGRepresentation(imageToSave, 1.0);
  CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)dataOfImageFromGallery, NULL);

    NSMutableDictionary* metaData = (NSMutableDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(imageSource,0,NULL)); //--> metaData dictionary without ITPC key

    if (imageSource == NULL) {
        // Error loading image
        NSLog(@"ERROR while loading screenshot image: %@",error);
        return;
    }

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
                             nil];
    CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);


    CFDictionaryRef exif = CFDictionaryGetValue(imageProperties, kCGImagePropertyExifDictionary);

    if (exif) {
        NSString *dateTakenString = (NSString *)CFDictionaryGetValue(exif, kCGImagePropertyExifDateTimeOriginal);
        NSLog(@"Date Taken: %@", dateTakenString);
    }

    // set image name and keywords in IPTC metadata
    NSString *iptcKey = (NSString *)kCGImagePropertyIPTCDictionary;

    NSMutableDictionary *iptcMetadata = metaData[iptcKey];
    iptcMetadata[(NSString *)kCGImagePropertyIPTCObjectName] = @"ImageTitle";
    iptcMetadata[(NSString *)kCGImagePropertyIPTCKeywords] = @"some keywords";
    metaData[iptcKey] = iptcMetadata; 

    //write the image data to the assets library (camera roll)
    ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];
    [library writeImageToSavedPhotosAlbum:imageToSave.CGImage metadata:metaData completionBlock:nil];

问题:metaData词典仅在具有键的对象下方

Problem : The metaData dictionary has got only below objects with keys

在metaData中的kCGImagePropertyIPTCDictionary为空的地方!请让我知道在哪里可以找到键{IPTC}的值?

Where kCGImagePropertyIPTCDictionary in the metaData is emtpy! Please let me know where I can find value for key {IPTC} ?

更新:引用来自此处

推荐答案

有点晚了,但这是我的方法:

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

使用类方法拥有一个类(我们称其为MetadataClass):

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

我如何使用它:

    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];

您现在可以创建一个NSData对象,其中包含图像的JPEG表示形式,包括元数据:

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];

这篇关于UIImage的元数据字典没有IPTC密钥的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆