views:

202

answers:

3

Hey everyone,

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: 

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 at http://www.markj.net/iphone-asynchronous-table-image/comment-page-1/#comments. His YellowJacket.zip code is very nice.

Nir Levy
Anyway, I think the Apple code http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html#//apple_ref/doc/uid/DTS40009394is by far more efficient than markj's one ;) so I think I'll stick to it :D
gotye
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
A: 

Thanks,

I'll have a more precise look later on.

However, just got another question to solve :D

What is the method below use for ?

- (void)connection:(NSURLConnection *)theConnection
     didReceiveData:(NSData *)incrementalData {
    if (data==nil) {
          data =
          [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:incrementalData];
}

Isn't the system doing it by itself ? Can't I just use

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection;

?

gotye
first, you should really post a different question for this (see SO FAQ). Second, no, when using async connections you really have to deal with getting the data yourself. The code you posted is good. once all the data is finished you will get a callback to connectionDidFinishLoading
Nir Levy
That would be why I didn't have to care about it last time ... cause it was a synchronous download ... Well, I think I understand the purpose of this method now ;) Cheers
gotye
+1  A: 

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