Inherited NSCoding adherence worthless in UIImageView
It seems to be an interesting bit of dodgy coding by Apple here.
My understanding of protocol adherence in general, and for NSCoding in particular, it is essential that if the superclass adhears to it, then the subclass must do too.
However, Apple seem to have forgotten that with UIImageView, as although according to the letter of protocol adhearence, the UIImageView does implement encodeWithCoder, it doesn't work if there is an attached UIImage object, which correct me if I'm wrong, is the whole point of UIImageView!
What makes this worse, is that the failure to properly implement NSCoding in UIImageView makes the implementation for UIView utterly useless unless you can be certain there are no UIImageViews in the subtree.
I don't think UIImageView can do any better than it does. Since it does not know where the UIImage comes from, there's no way for it to know what to code. It would be wasteful for it to code the actual bytes of the image, since you probably already have it saved to a file somewhere.
It seems like a simple solution is to subclass UIImageView. Then your code would know where the image came from, and how to read it back.
I don't think UIImageView can do any better than it does. Since it does not know where the UIImage comes from, there's no way for it to know what to code. It would be wasteful for it to code the actual bytes of the image, since you probably already have it saved to a file somewhere.
It seems like a simple solution is to subclass UIImageView. Then your code would know where the image came from, and how to read it back.
You make a valid point there, but if that is the case, the UIImageView should have a encodeWithCoder method that specifically excludes saving the image, and it should be documented as such.
Simply allowing it to call the encodeWithCoder of the UIImage (which of course doesn't exist) causes the encoding to fail, and means that you can't encode UIImageView, or any UIView contaning it, which is my argument.
When you say it "fails" no one knows what you mean. I assumed it failed to encode correctly. I think you are saying it causes a crash due to an exception. I haven't had a chance to try it myself.
When you say it "fails" no one knows what you mean. I assumed it failed to encode correctly. I think you are saying it causes a crash due to an exception. I haven't had a chance to try it myself.
Sorry, you're right - I'm not being clear.
It crashes because UIImage doesn't implement encodeWithCoder. This means I can't save my view tree, which I'd hoped I could, as I want to be able to reload it quickly, rather than reconstructing it from scratch, which is quite slow.
I can't currently think of any other way storing the view tree.
The only solution I can see for you is to subclass UIImageView. It's a pain, because it sounds like you might have to write the encode and decode yourself entirely.
You might also be able to add a category to UIImage to implement encodeWithCode. I've never tried it, but it might work.