7

In my app I am retrieving a UIImage from the asset library, this image has meta data. The app is then resizing and rotating the image which in turn is creating a new image. The new image does not have the original meta data which is expected but how do I add the meta data back to the image before upload?

Thanks in advance!

shoughton123
  • 4,255
  • 3
  • 21
  • 23

1 Answers1

14

Fixed it myself, here is a method I used just incase anyone else is wondering 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;
}
shoughton123
  • 4,255
  • 3
  • 21
  • 23
  • #import is needed to get the constants – Anth0 May 07 '13 at 08:59
  • where did you create ALAsset and how are you using it? code you provide the code for that as well? – Burhanuddin Sunelwala Aug 28 '13 at 09:04
  • Hi, the ALAsset is coming from an image picker earlier in the app. Please see apples documentation for using an image picker and working with ALAssets: https://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html or see the ELCImagePickerController: https://github.com/B-Sides/ELCImagePickerController. I have used my own custom image picker to allow advanced features but for simple image library selection either of these will do. Thanks Sam. – shoughton123 Aug 29 '13 at 14:01
  • Is creating the blank dictionaries for all the metadata keys necessary? What happens if I just use the metadata dictionary straight from the asset representation? – jrturton Feb 26 '14 at 12:42
  • This method does work, however it allocates a lot of memory, that is not released properly. Be careful, app could be killed by iOS due to high memory pressure. – Rostyslav Jun 12 '14 at 13:01
  • I am using the same code as you are, and I am noticing that my destination image is always about 3 times smaller than original. Do you have this problem? – AntonijoDev May 27 '16 at 08:14