Can Leloğlu
Can Leloğlu

Reputation: 271

How to set metadata for kCGImagePropertyRawDictionary on iOS?

Hi I am trying to set custom information in an image. The documentation here: https://developer.apple.com/reference/imageio/kcgimagepropertyrawdictionary says that this is a dictionary for minimal data.

What I understand from this is I can store some string or NSData inside an image.

iOS does not allow setting metadata which is not listed in CGImageProperties. I could not find any possible keys under kCGImagePropertyRawDictionary. I also tried setting this to a string directly. All those have failed.

Does anyone know how to set the value of kCGImagePropertyRawDictionary?

Upvotes: 1

Views: 499

Answers (1)

ravron
ravron

Reputation: 11211

What I understand from this is I can store some string or NSData inside an image.

Yes, the purpose of image metadata is to store, well, image metadata. That can include numbers, booleans, and – most commonly – strings. However, the metadata system isn't designed for arbitrary metadata – each value has a specific purpose. To enforce this purpose, CGImageDestinationAddImage silently drops any values that it believes are invalid. There is a fixed set of keys allowable in the properties dictionary passed to that function, which is the union of the destination properties and the CGImageProperties.

You have correctly noted that kCGImagePropertyRawDictionary is one such allowable key. However, its value must itself be a dictionary, and the documentation does not specify what keys are acceptable there – if it did, that would be listed on the CGImageProperties page as "RAW Dictionary Keys." Therefore, there are no allowable keys for that dict. If you want to store data about an image that doesn't fit into one of the (many) categories described in CGImageProperties, you'll have to store it elsewhere.


Just for fun, since I was curious, I ran strings on the ImageIO framework, and found a list of ~300 string constants that looked like possible keys for this dict. I wrote a quick test program to try to put each one of those keys into a dict at the kCGImagePropertyRawDictionary key. Not a single one made it through. This makes me think that the kCGImagePropertyRawDictionary is just there for future use, and currently will not accept any metadata.

Upvotes: 4

Related Questions