Is there a way to read PNG comments in iOS without doing manual data parsing? I have PNG files that store metadata that I would like to a开发者_运维百科ccess when I load them to a UIImage object, but I haven't been able to find any easy way to do this.
There doesn't seem to be any easy way to do it; I don't see it mentioned in the list of properties available from a CGImageSource, for example. And there certainly isn't a way to do it from a UIImage, that class doesn't (publicly) preserve any metadata beyond the EXIF orientation flag, and that only so the code doesn't have to rotate the image when reading it.
Fortunately PNG is well-documented and very easy to parse, so extracting the zTXt chunks shouldn't be much of a problem.
Any data you can get to "easily" without writing your own data parser is going to be inside the image properties dictionary.
UIImage* image; // assuming this exists
NSData* pngData = UIImagePNGRepresentation(image);
CGImageSourceRef imgSrc = CGImageSourceCreateWithData(pngData,NULL);
CFDictionaryRef imgSrcProps = CGImageSourceCopyProperties(imgSrc,NULL);
NSDictionary* imgSrcPropDict = (NSDictionary*)imgSrcProps;
NSLog(@"image properties: %@", imgSrcPropDict);
CFRelease(imgSrcProps);
CFRelease(imgSrc);
Hope this helps, not sure if what you are looking for is in there
精彩评论