akaru
akaru

Reputation: 6317

How to inject exif metadata into an image, without copying the image?

I have previously asked this question: How to write exif metadata to an image.

I now have found a way to inject metadata. However, it results in a copy of the image into memory. With large images, and the need to already have a copy in memory, this is going to have performance, and possibly cause a memory crash.

Is there a correct way to inject metadata without having to make a copy of the image? Perhaps it could be tacked on to a file, after it is written to disk?

I would prefer native implementations, without having to resort to a third party library just for this, if at all possible.

Upvotes: 4

Views: 5028

Answers (3)

Alex L
Alex L

Reputation: 8449

CGImageSourceRef could be used to get image properties including its thumbnail without loading all image data into memory. This way memory is not wasted by UIImage and NSData.

CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], NULL);

Then save CGImageDestinationRef adding the source image and exif data.

CGImageDestinationAddImageFromSource (destRef,    
                                      imageSource,
                                      0,
                                     (CFDictionaryRef)propertes );//exif
BOOL success = CGImageDestinationFinalize(destRef);

Upvotes: 0

XTL
XTL

Reputation: 851

I would expect the existing exif manipulator software can do it, but haven't tested.

Links:

Upvotes: 0

BitBank
BitBank

Reputation: 8715

This question could require a small or large amount of code depending on what you need. EXIF data is stored in a JPEG APP1 marker (FFE1). It looks very much like a TIFF file with a TIFF header, IFD and individual tags with the data. If you can build your own APP1 marker segment, then inserting it or replacing it in a JPEG file is trivial. If you are looking to read the metadata from an existing file, add some new tags and then write it back, that can be more involved. The tricky part of EXIF data are those tags which require more than 4-bytes. Each TIFF tag is 12 bytes: 2-byte tag, 2-byte data type, 4-byte count, 4-byte data. If the data doesn't fit completely in the 4 bytes of the tag, then the tag specifies an absolute offset into the file of where to find the data. If the existing data has any tags with data like this (e.g. make, model, capture date, capture time, etc), you will need to repack that data by fixing the offsets and then add your own. In a nutshell:

1) If you are adding a pre-made APP1 marker to a JPEG file, this is simple and requires little code.

2) If you need to read the existing meta-data from a JPEG file, add your own and write it back, the code is a bit more involved. It's not "difficult", but it involves a lot more than just reading and writing blocks of data.

Start by reading the TIFF 6.0 spec to understand the tag and directory structure:

TIFF 6.0 spec

Next, take a look at the JPEG EXIF spec:

EXIF 2.2 Spec

Upvotes: 1

Related Questions