2

I'm struggling to write a string into a JPG's Exif: user comments section, that contains the degress symbol (°).

But it constantly writes out a '?' character in its place.

ex.

NSString *xmlData = [[NSString alloc] initWithUTF8String:"°,\xC2\xB0,\u00b0"];
[EXIFDictionary setObject:xmlData forKey:(NSString*)kCGImagePropertyExifUserComment];

Results in:

?,?,?

This doesn't work either:

NSString *xmlData = [[NSString alloc] initWithFormat:@"43, °, \xC2\xB0, \u00b0, 21'45\""];

NSLog displays the '°' of course. But when I use an EXIF viewer to actually read the data saved within the JPG, it still comes out as '?'.

Some more info. Once the properties are written, ex.

[metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];

The result from NSLog looks like this:

UserComment = "43, \U00b0, \U00b0, \U00b0, 21'45\"";

Has anyone had success with this?

Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57
  • have you tried simply as string no utf8/16 etc – Anoop Vaidya May 01 '13 at 15:48
  • How are you printing the result? – rob mayoff May 01 '13 at 15:49
  • 2
    `\Unnnn` is the usual NSLog output for non-ASCII characters in a dictionary, so `\U00b0` is the same as `°`. - I *assume* that the problem is that EXIF does not have a defined encoding. Perhaps iOS writes the comment as UTF-8 and your EXIF viewer interprets it as something else or displays all non-ASCII characters as "?". – Martin R May 01 '13 at 16:43
  • I have tried a variety of EXIF viewers on the iPhone and desktop, and they all report the same way it seems. – Sebastian Dwornik May 01 '13 at 17:42

1 Answers1

1

This seems to be a bug or unsupported in the ImageIO framework.

The Exif specification (http://www.exif.org/Exif2-2.PDF) states that the UserComment tag starts with a 8-byte area specifying the encoding and can be ASCII, JIS, Unicode or Undefined.

If I set a UserComment using ExifTool (on OS X):

exiftool -UserComment="30°" test.jpg

and inspect the file "test.jpg" with a hex editor, I find the following data for the UserComment tag:

55 4e 49 43 4f 44 45 00     // UNICODE id
00 33 00 30 00 b0           // "30°" as (big-endian) UTF-16

On the other hand, if I use the code from your older question Modified EXIF data doesn't save properly to set the same UserComment, I find the following data:

41 53 43 49 49 00 00 00     // ASCII id
33 30 3F                    // "30?" in ASCII encoding

This means that ImageIO/CoreGraphics uses only the ASCII encoding to save UserComment tags and converts all non-ASCII characters to a question mark.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Your clarifying example is brilliant. And explains why this hack works: http://stackoverflow.com/a/8505114/7599 So what remains is for me to try and convert my NSString text into ASCII now and hope the results stick. – Sebastian Dwornik May 02 '13 at 00:32
  • No success. All of my hacking still did not result in the degree symbol being saved in ASCII format. – Sebastian Dwornik May 02 '13 at 02:28
  • @SebastianDwornik: I don't see how the code from your linked answer could help (and I have the impression that the answer was accepted even if it did not solve the problem.) - The problem is that `CGImageDestinationAddImageFromSource` does not handle non-ASCII characters in the Exif UserComment correctly. And the degree symbol is not an ASCII character, so "saving the degree symbol in ASCII format" does not make sense. - I am afraid that there is no solution using the ImageIO framework on iOS. – Martin R May 02 '13 at 08:54