Search code examples
iosimagensdataexif

Convert image with exif to NSData type


I have a question on how to convert an image with exif data to NSData type. I get the image rather capture an image in camera or get the image from camera roll.

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

After picking the image, I went here to convert the image with exif to NSData and sent to the sever database. However, I tried many approach but it fails. When I get the picture file in database, it lost all the exit and metadata information of the picture.

So, I want to ask, is there any way to convert an image and keep the exif and metadata in NSData Type?

Thanks all.


Solution

  • You can do something like that, but if you don't need the NSData for further operation I strongly suggest you to save the image on disk:

                NSDictionary *metadata = [info objectForKey:    UIImagePickerControllerMediaMetadata];
                CFURLRef url = (__bridge_retained CFURLRef)[NSURL fileURLWithPath:path];
                CFMutableDictionaryRef metadataImage = (__bridge_retained CFMutableDictionaryRef) metadata;
                NSMutableData * destData = [NSMutableData data];   
                CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)destData,kUTTypeJPEG,1,NULL);
                CGImageDestinationAddImage(destination, uiImage.CGImage, metadataImage);
                if (!CGImageDestinationFinalize(destination)) {
                    DLog(@"Failed to write image to %@", path);
                }
                else {
                    DLog(@"Writing image to %@", path);
    
                }