How to save image data with exif information from UIImagePickerController

The UIImage returned from UIImagePickerController is without exif information, if we want generate an NSData from it, saving to disk or sharing via network for example. We can mix the exif information into the NSData.

Like this:

- (NSData *)dataFromImage:(UIImage *)image metadata:(NSDictionary *)metadata mimetype:(NSString *)mimetype
    NSMutableData *imageData = [NSMutableData data];
    CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)mimetype, NULL);
    CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, uti, 1, NULL);

    if (imageDestination == NULL)
        NSLog(@"Failed to create image destination");
        imageData = nil;
        CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)metadata);

        if (CGImageDestinationFinalize(imageDestination) == NO)
            NSLog(@"Failed to finalise");
            imageData = nil;


    return imageData;

and in the callback of UIImagePickerController, do this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
    NSString *mimeType = @"image/jpeg"; // ideally this would be dynamically set. Not defaulted to a jpeg!

    // you could add to the metadata dictionary (after converting it to a mutable copy) if you needed to.

    // convert to NSData
    NSData *imageData = [self dataFromImage:image metadata:metadata mimetype:mimeType];

    // ...


“How to save image data with exif information from UIImagePickerController”的3个回复

  1. 大神,我想问一下,我也是用CGImageDestinationRef去设置图片的exif和tiff信息的,但是为什么我保存不进去呢,我是先把原图压缩,再保存的。


电子邮件地址不会被公开。 必填项已用*标注