I'm trying to add some information to a UIImage I am doing some experimentation with to add details like the number of UILabel's present on the screen and their frames.
I tried using setValue:forKeyPath: but this does not work since UIImage is not key-value compliant. What is an efficient way to do this?
Adding metadata using the CGImageMetadata API's seems to need me to convert the UIImage to NSData. Also, I would like to access the data without saving it every time. Can anyone point me to a good way to do this?
You can get use of associated objects to complement objects of classes source of which you don't have access to with some additional data (with use of
objc_setAssociatedObjectto set andobjc_getAssociatedObjectto read the data):For convenience it can be effectively wrapped with a property by extending the class with a category:
P.S. It's not correct to say that
UIImageis not KVC compliant only because it doesn't supportsetValue:forKeyPath:for non-existing key paths. It's perfectly compliant for key-paths the class has defined: