it depends on how many images you'll need to request at any given time. a good solution i found for industrial strength design/requests was to create a request funnel (which reduced the number of images). these requests supported cancellation (for when the image view goes off-screen), and were all handled via an NSOperationQueue. there was a ton of threading work (read: not for the noob) to get it seamless, but it worked great for large tables which had many images (since the ceiling is quite low for the number of images you can fit into physical memory on the iOS device with the least amount of memory).
that's one 'right' way, if you have a lot of images to download. if not, then you can look into implementation using performSelectorInBackground:…, and just let the object perform its own locking/handling. either way, you'll have to do something/display something while the image is being downloaded, so the calling thread (typically the main thread) is not blocked while the image is received.
Q::follow-up: thanks but where should i declare this method?? inside if (sections == 3) { } – user437503
A::it will take the `general' form:
- (void)setCellImage:(UIImage *)img {
/* assert here if called from secondary thread */
myImageView.image = img;
}
- (void)udpateImageFollowingTap {
NSAutoreleasePool * pool = [NSAutoreleasePool new];
NSString * imageUrl = [self.a1 objectAtIndex:self.tap];
UIImage * tmpImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
[self performSelectorOnMainThread:@selector(setCellImage:) withObject:tmpImage];
[tmpImage release];
[pool release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger sections = [indexPath section];
if (sections == 3) {
ltxt.text = [NSString stringWithFormat:@" %d of %d", tap,[a1 count]];
if(tap<[a1 count]-1) {
tap++;
/* insert code to discard previous image and display loading image indication here */
[self performSelectorInBackground:@selector(udpateImageFollowingTap) withObject:0];
}
}
}