3

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.

gotye
  • 958
  • 2
  • 14
  • 33

2 Answers2

2

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.

Mongus Pong
  • 11,337
  • 9
  • 44
  • 72
1

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.

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
Nir Levy
  • 4,613
  • 2
  • 34
  • 47
  • Anyway, I think the Apple code http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html#//apple_ref/doc/uid/DTS40009394 is by far more efficient than markj's one ;) so I think I'll stick to it :D – gotye Mar 16 '10 at 09:48
  • Apple's code tightly couples the image downloading and the table view. I think you should re-consider using a pattern where your images are self sufficient and can both download the file, cache it, and display the image. It will make it more reusable as well as easier to understand when someone (you, a few months from now :) looks at the table view. – Nir Levy Mar 16 '10 at 10:14