UIImage Metadata

In my application, I retrieve UIImage from the asset library, this image has metadata. The application then resizes and rotates the image, which in turn creates a new image. The new image does not have the original metadata that is expected, but how to add metadata back to the image before uploading?

Thanks in advance!

+7
source share
3 answers

I fixed it myself, here is the method I used, just making someone else wonder how to do it! :)

-(UIImage *)addMetaData:(UIImage *)image { NSData *jpeg = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]; CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)jpeg, NULL); NSDictionary *metadata = [[asset_ defaultRepresentation] metadata]; NSMutableDictionary *metadataAsMutable = [metadata mutableCopy]; NSMutableDictionary *EXIFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary]; NSMutableDictionary *GPSDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary]; NSMutableDictionary *TIFFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyTIFFDictionary]; NSMutableDictionary *RAWDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyRawDictionary]; NSMutableDictionary *JPEGDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyJFIFDictionary]; NSMutableDictionary *GIFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGIFDictionary]; if(!EXIFDictionary) { EXIFDictionary = [NSMutableDictionary dictionary]; } if(!GPSDictionary) { GPSDictionary = [NSMutableDictionary dictionary]; } if (!TIFFDictionary) { TIFFDictionary = [NSMutableDictionary dictionary]; } if (!RAWDictionary) { RAWDictionary = [NSMutableDictionary dictionary]; } if (!JPEGDictionary) { JPEGDictionary = [NSMutableDictionary dictionary]; } if (!GIFDictionary) { GIFDictionary = [NSMutableDictionary dictionary]; } [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary]; [metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary]; [metadataAsMutable setObject:TIFFDictionary forKey:(NSString *)kCGImagePropertyTIFFDictionary]; [metadataAsMutable setObject:RAWDictionary forKey:(NSString *)kCGImagePropertyRawDictionary]; [metadataAsMutable setObject:JPEGDictionary forKey:(NSString *)kCGImagePropertyJFIFDictionary]; [metadataAsMutable setObject:GIFDictionary forKey:(NSString *)kCGImagePropertyGIFDictionary]; CFStringRef UTI = CGImageSourceGetType(source); NSMutableData *dest_data = [NSMutableData data]; CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL); //CGImageDestinationRef hello; CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable); BOOL success = NO; success = CGImageDestinationFinalize(destination); if(!success) { } dataToUpload_ = dest_data; CFRelease(destination); CFRelease(source); return image; } 
+14
source

Take a look at this answer. I think he will point you in the right direction.

Email jpg created in the app, including metadata

+3
source

Here you go to the Git Hub Project and some links to tutorials Adding metadata for iOS images is an easy way , Retrieving metadata from images in iOS . Best wishes;)

+3
source

All Articles