Is there any reason why we don't use subclasses of UIImageView?
I'm currently trying to create a subclass of UIImageView in order to make it download its image from server asynchronously ;)
I 开发者_高级运维tried to do it by myself but I haven't gone very far yet :D
Anyway, I looked around here and found this :
AsyncImageDownload
I had a look at the code and the first which springs to mind is : why subclassing a UIView and not a UIImageView ?!?
Any thoughts ?
Cheers mates,
Gotye.
A part of the Gang of Fours design pattern philosophy is to
"Favor 'object composition' over 'class inheritance'."
This reduces the tight coupling between ojects. Then changing one class will have less impact on the other classes in the system. This makes changes easier, resulting in a more stable, easy to maintain system.
In this case, as a previous poster mentioned, it allows the image to do other things as well, such as display a progress indicator.
The reason it is subclassing UIView is so that you should, for example, display a UIActivityIndicator while the image is being downloaded. They do not show this in their example but I have used this code and it is really good. Also look at the comments for this post you will find more code examples, also including some caching and nice stuff.
Take a look at Wayne Cochra's comment. His YellowJacket.zip code is very nice.
精彩评论