1

I have successfully added metadata to a jpg created within the app and saved it to the Camera Roll using the

writeImageToSavedPhotosAlbum: metadata: completionBlock: 

method. However I would also like the option of emailing this jpg with the metadata (such as location, exit, etc.). I use this to email:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSData *myData =  UIImageJPEGRepresentation(emailImage, 0.8);
[picker addAttachmentData:myData mimeType:@"image/jpg" fileName:@"photo"];

However, this results in no metadata.
When the saved image is sent via Apple's photo app, metadata is included. Is there a way to embed the metadata into NSData attachment? Any ideas?

Ted
  • 23
  • 1
  • 5
  • Are you sure that the Image still contains the meta data _after_ you create the UIImageJPEGRepresentation? Edit: [UIImageJPEGRepresentation seems to purge meta data](http://stackoverflow.com/questions/5173764/from-the-results-of-uiimagepickercontrollehow-how-do-i-get-jpeg-with-metadata-in) – ff10 Sep 15 '11 at 14:31
  • Yes, UIImageJPEGRepresentation purges metadata. That is the problem I am trying to solve. I have created an image from within the app and now want to email it with GPS metadata. I can successfully add back the dictionaries when I writeImageToSavedPhotosAlbum:, however I want to email the new image directly from the app. – Ted Sep 16 '11 at 08:34

2 Answers2

7

UIImage doesn't hold any metadata. If you have the path for the image read data directly from it. If you get the image back from camera roll there's the imagePickerController:didFinishPickingMediaWithInfo: method from UIImagePickerDelegate which also contains the metadata inside the info dictionary.

Also the mimeType should be "image/jpeg".

Edit: To add metadata to a UIImage you can use the ImageIO framework: You can create a CGImageDestination object from a UIImage, add metadata to it using CGImageDestinationSetProperties and then get the raw data (which includes the compressed image and the metadata) from it

alex-i
  • 5,406
  • 2
  • 36
  • 56
  • Thanks for the response. Is there a way to add metadata to an image and email it without the user retrieving the image from the imagePickerController? i.e. creating an image from within the app and emailing it with GPS metadata? Thanks. – Ted Sep 16 '11 at 08:31
  • 1
    You should take a look at [ImageIO framework](http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/ImageIORefCollection/_index.html) for that. You can add metadata to `CGImageDestination` using `CGImageDestinationSetProperties` and then get the raw data from it – alex-i Sep 16 '11 at 09:09
  • I ticked your answer. Though the answer was really in your last comment. – Ted Sep 16 '11 at 16:12
0

Here is an example code to attach metadata to a NSMutableData object, which can be mailed.

UIImage* yourImage = ... from somewhere ...
NSDictionary* info = ... in my case from didFinishPickingMediaWithInfo:

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) UIImageJPEGRepresentation(yourImage, 0.5 /* compression factor */), NULL);

NSMutableData* imageData = [NSMutableData data];

CGImageDestinationRef imageDest = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, kUTTypeJPEG, 1, NULL);
CGImageDestinationAddImageFromSource(imageDest, source, 0, (__bridge CFDictionaryRef)info[@"UIImagePickerControllerMediaMetadata"]);
CGImageDestinationFinalize(imageDest);

CFRelease(imageDest);
CFRelease(source);

// at this point, imageData will contain your image + metadata
David
  • 9,635
  • 5
  • 62
  • 68