Le dictionary de métadonnées de UIImage n'a pas de valeur pour la key IPTC

Tout en sauvegardant l'image de la capture d'écran, j'essaye de changer les méta-données de l'object UIImage pour le titre personnalisé et la description de l'image.

En-têtes importés

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

Dans ma méthode

  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], (NSSsortingng *)kCGImageSourceShouldCache, nil]; CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options); CFDictionaryRef exif = CFDictionaryGetValue(imageProperties, kCGImagePropertyExifDictionary); if (exif) { NSSsortingng *dateTakenSsortingng = (NSSsortingng *)CFDictionaryGetValue(exif, kCGImagePropertyExifDateTimeOriginal); NSLog(@"Date Taken: %@", dateTakenSsortingng); } // set image name and keywords in IPTC metadata NSSsortingng *iptcKey = (NSSsortingng *)kCGImagePropertyIPTCDictionary; NSMutableDictionary *iptcMetadata = metaData[iptcKey]; iptcMetadata[(NSSsortingng *)kCGImagePropertyIPTCObjectName] = @"ImageTitle"; iptcMetadata[(NSSsortingng *)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]; 

Problème : Le dictionary metaData n'a que des objects en dessous avec des keys entrez la description de l'image ici

kCGImagePropertyIPTCDictionary dans les méta-données est emtpy! S'il vous plaît laissez-moi savoir où je peux find de la valeur pour la key {IPTC} ?

MISE À JOUR: Référence prise d'ici

Un peu tard, mais voici comment je l'ai fait:

Avoir une class (appelons-le MetadataClass ) avec la méthode de class:

 + (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( NSSsortingng *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 addEnsortingesFromDictionary:metadata]; } else if( [metadata isKindOfClass:NSSsortingng.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); } 

Et comment je l'utilise:

  NSDictionary *iptc = @{ (NSSsortingng *)kCGImagePropertyIPTCObjectTypeReference : @"kCGImagePropertyIPTCObjectTypeReference", (NSSsortingng *)kCGImagePropertyIPTCObjectAtsortingbuteReference : @"kCGImagePropertyIPTCObjectAtsortingbuteReference", (NSSsortingng *)kCGImagePropertyIPTCObjectName : @"kCGImagePropertyIPTCObjectName", (NSSsortingng *)kCGImagePropertyIPTCEditStatus : @"kCGImagePropertyIPTCEditStatus", (NSSsortingng *)kCGImagePropertyIPTCEditorialUpdate : @"kCGImagePropertyIPTCEditorialUpdate", (NSSsortingng *)kCGImagePropertyIPTCUrgency : @"kCGImagePropertyIPTCUrgency", (NSSsortingng *)kCGImagePropertyIPTCSubjectReference : @"kCGImagePropertyIPTCSubjectReference", (NSSsortingng *)kCGImagePropertyIPTCCategory : @"kCGImagePropertyIPTCCategory", (NSSsortingng *)kCGImagePropertyIPTCSupplementalCategory : @"kCGImagePropertyIPTCSupplementalCategory", (NSSsortingng *)kCGImagePropertyIPTCFixtureIdentifier : @"kCGImagePropertyIPTCFixtureIdentifier", (NSSsortingng *)kCGImagePropertyIPTCKeywords : @"kCGImagePropertyIPTCKeywords", (NSSsortingng *)kCGImagePropertyIPTCContentLocationCode : @"kCGImagePropertyIPTCContentLocationCode", (NSSsortingng *)kCGImagePropertyIPTCContentLocationName : @"kCGImagePropertyIPTCContentLocationName", (NSSsortingng *)kCGImagePropertyIPTCReleaseDate : @"kCGImagePropertyIPTCReleaseDate", (NSSsortingng *)kCGImagePropertyIPTCReleaseTime : @"kCGImagePropertyIPTCReleaseTime", (NSSsortingng *)kCGImagePropertyIPTCExpirationDate : @"kCGImagePropertyIPTCExpirationDate", (NSSsortingng *)kCGImagePropertyIPTCExpirationTime : @"kCGImagePropertyIPTCExpirationTime", (NSSsortingng *)kCGImagePropertyIPTCSpecialInstructions : @"kCGImagePropertyIPTCSpecialInstructions", (NSSsortingng *)kCGImagePropertyIPTCActionAdvised : @"kCGImagePropertyIPTCActionAdvised", (NSSsortingng *)kCGImagePropertyIPTCReferenceService : @"kCGImagePropertyIPTCReferenceService", (NSSsortingng *)kCGImagePropertyIPTCReferenceDate : @"kCGImagePropertyIPTCReferenceDate", (NSSsortingng *)kCGImagePropertyIPTCReferenceNumber : @"kCGImagePropertyIPTCReferenceNumber", (NSSsortingng *)kCGImagePropertyIPTCDateCreated : @"kCGImagePropertyIPTCDateCreated", (NSSsortingng *)kCGImagePropertyIPTCTimeCreated : @"kCGImagePropertyIPTCTimeCreated", (NSSsortingng *)kCGImagePropertyIPTCDigitalCreationDate : @"kCGImagePropertyIPTCDigitalCreationDate", (NSSsortingng *)kCGImagePropertyIPTCDigitalCreationTime : @"kCGImagePropertyIPTCDigitalCreationTime", (NSSsortingng *)kCGImagePropertyIPTCOriginatingProgram : <CFBundleName>, (NSSsortingng *)kCGImagePropertyIPTCProgramVersion : <CFBundleVersion>, (NSSsortingng *)kCGImagePropertyIPTCObjectCycle : @"kCGImagePropertyIPTCObjectCycle", (NSSsortingng *)kCGImagePropertyIPTCByline : @"kCGImagePropertyIPTCByline", (NSSsortingng *)kCGImagePropertyIPTCBylineTitle : @"kCGImagePropertyIPTCBylineTitle", (NSSsortingng *)kCGImagePropertyIPTCCity : @"kCGImagePropertyIPTCCity", (NSSsortingng *)kCGImagePropertyIPTCSubLocation : @"kCGImagePropertyIPTCSubLocation", (NSSsortingng *)kCGImagePropertyIPTCProvinceState : @"kCGImagePropertyIPTCProvinceState", (NSSsortingng *)kCGImagePropertyIPTCCountryPrimaryLocationCode : @"kCGImagePropertyIPTCCountryPrimaryLocationCode", (NSSsortingng *)kCGImagePropertyIPTCCountryPrimaryLocationName : @"kCGImagePropertyIPTCCountryPrimaryLocationName", (NSSsortingng *)kCGImagePropertyIPTCOriginalTransmissionReference : @"kCGImagePropertyIPTCOriginalTransmissionReference", // (NSSsortingng *)kCGImagePropertyIPTCHeadline: : @"kCGImagePropertyIPTCHeadline", (NSSsortingng *)kCGImagePropertyIPTCCredit : @"kCGImagePropertyIPTCCredit", (NSSsortingng *)kCGImagePropertyIPTCSource : @"kCGImagePropertyIPTCSource", (NSSsortingng *)kCGImagePropertyIPTCCopyrightNotice : @"kCGImagePropertyIPTCCopyrightNotice", (NSSsortingng *)kCGImagePropertyIPTCContact : @"kCGImagePropertyIPTCContact", (NSSsortingng *)kCGImagePropertyIPTCCaptionAbstract : @"kCGImagePropertyIPTCCaptionAbstract", (NSSsortingng *)kCGImagePropertyIPTCWriterEditor : @"kCGImagePropertyIPTCWriterEditor", (NSSsortingng *)kCGImagePropertyIPTCImageType : @"kCGImagePropertyIPTCImageType", (NSSsortingng *)kCGImagePropertyIPTCImageOrientation : @"kCGImagePropertyIPTCImageOrientation", (NSSsortingng *)kCGImagePropertyIPTCLanguageIdentifier : @"kCGImagePropertyIPTCLanguageIdentifier", (NSSsortingng *)kCGImagePropertyIPTCStarRating : @"kCGImagePropertyIPTCStarRating", (NSSsortingng *)kCGImagePropertyIPTCCreatorContactInfo : @"kCGImagePropertyIPTCCreatorContactInfo", (NSSsortingng *)kCGImagePropertyIPTCRightsUsageTerms : @"kCGImagePropertyIPTCRightsUsageTerms", (NSSsortingng *)kCGImagePropertyIPTCScene : @"kCGImagePropertyIPTCScene", (NSSsortingng *)kCGImagePropertyIPTCContactInfoCity : @"kCGImagePropertyIPTCContactInfoCity", (NSSsortingng *)kCGImagePropertyIPTCContactInfoCountry : @"kCGImagePropertyIPTCContactInfoCountry", (NSSsortingng *)kCGImagePropertyIPTCContactInfoAddress : @"kCGImagePropertyIPTCContactInfoAddress", (NSSsortingng *)kCGImagePropertyIPTCContactInfoPostalCode : @"kCGImagePropertyIPTCContactInfoPostalCode", (NSSsortingng *)kCGImagePropertyIPTCContactInfoStateProvince : @"kCGImagePropertyIPTCContactInfoStateProvince", (NSSsortingng *)kCGImagePropertyIPTCContactInfoEmails : @"kCGImagePropertyIPTCContactInfoEmails", (NSSsortingng *)kCGImagePropertyIPTCContactInfoPhones : @"kCGImagePropertyIPTCContactInfoPhones", (NSSsortingng *)kCGImagePropertyIPTCContactInfoWebURLs : @"kCGImagePropertyIPTCContactInfoWebURLs" }; NSDictionary<NSSsortingng *, NSNumber *> *orientation = @{(NSSsortingng *)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[(NSSsortingng *)kCGImagePropertyIPTCDictionary] = iptc; [d addEnsortingesFromDictionary:orientation]; [MetadataClass addMetadata:d toImageDataSampleBuffer:imageDataSampleBuffer]; 

Vous pouvez maintenant créer un object NSData contenant la représentation JPEG de l'image, y compris les métadonnées:

 NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer] 

Et puis, vous pouvez save le file en utilisant:

 [jpegData writeToFile:<path of JPEG file> atomically:YES];