Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you copy UIImage exif data to a scaled UIImage?

Tags:

ios

uiimage

exif

I am currently grabbing a photo when a user takes a picture:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   UIImage *image = info[UIImagePickerControllerOriginalImage];

   // create a jpeg
   NSData *jpegData = UIImageJPEGRepresentation(image, 1.0f);

   // write jpeg image to file in app space
   NSString *filePath = 

   // create file path in app space
   [imageData writeToFile:filePath atomically:NO];
}

This works great, the file is created a a jpeg with its EXIF data.

Now I would like to scale the image down a bit to make it a little smaller. However I would like to keep some or all of the EXIF data that existed in the original UIImage and copy it over to the scaled image.

Currently scaling the image:

UIImage *scaledImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext*_Nonnull myContext) {
   [image drawInRect:(CGRect) {.origin = CGPointZero, .size = size}];
}];

This creates a scaled image just fine, however it does not contain any EXIF data.

Is there a way to scale the image and retain the original image's EXIF data? Can I grab the EXIF data from the original image and copy it over to the scaled image?

Also I have searched through a lot of answers using ALAssetsLibrary which is now deprecated. Seems like the alternative is PhotoKit. Which states:

In iOS and macOS, PhotoKit provides classes that support building photo-editing extensions for the Photos app. In iOS and tvOS, PhotoKit also provides direct access to the photo and video assets managed by the Photos app.

However I am not using the Photos app, my image is not coming from the local photo library or icloud as I only want to store the photo in my private app space.

like image 720
lostintranslation Avatar asked Oct 21 '25 05:10

lostintranslation


1 Answers

The metadata from a camera capture using UIImagePickerController arrives in the info dictionary under the UIImagePickerControllerMediaMetadata key. It can be copied into the data for another UIImage using the ImageIO framework (you will need to import ImageIO). My code for this is Swift, but it uses Objective-C Cocoa classes and ImageIO C functions, so you should easily be able to translate it into Objective-C:

let jpeg = im!.jpegData(compressionQuality:1) // im is the new UIImage
let src = CGImageSourceCreateWithData(jpeg as CFData, nil)!
let data = NSMutableData()
let uti = CGImageSourceGetType(src)!
let dest = CGImageDestinationCreateWithData(data as CFMutableData, uti, 1, nil)!
CGImageDestinationAddImageFromSource(dest, src, 0, m) // m is the metadata
CGImageDestinationFinalize(dest)

After that, data is the data for the image im together with the metadata m from the capture.

like image 73
matt Avatar answered Oct 22 '25 18:10

matt