views:

923

answers:

5

Hi,

I'm using the three20 project for my iPhone app. I've narrowed my problem down and I'm now just trying to re-create the 'Web Images in Table' example that comes with the project. I've copied the code exactly as in the project, with the exception that I do not use the TTNavigator (which the example does) but I am adding my TTTableViewController manually to a tabBar.

The problem is as follows; the images in the table should load automatically from the web, like in the example. But they only load after I scroll the table up and down.

In the console it clearly says it is downloading the images, and you see the activity indicator spinning like forever.. And unless I scroll up and down once, the images will never appear.

Anyone? Thanks in advance.

P.S: If I'm using this code in any random UIView, It also doesn't work (only shows a black square):

TTImageView* imageView = [[[TTImageView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)] autorelease];
imageView.autoresizesToImage = YES;
imageView.URL = @"http://webpimp.nl/logo.png";
[self.view addSubview:imageView];

If I put this code in my AppDelegate (right onto the window), it does work .. strange?

POSSIBLE SOLUTION: Although I stopped using TTImageView for this purpose, I do think I found out what the problem was; threading (hence accepting the answer of Deniz Mert Edincik). If I started the asynchronous download (because basically that is all the TTImageView is, an asynchronous download) from anywhere BUT the main thread, it would not start. If I started the download on the main thread, it would start immediately..

A: 

When an image finishes loading try sending a reloadData message to the table view. This forces the table to recalculate the size of the rows and redraw the table. Just be careful that you don't start downloading the image again in response to this message.

Mark Thalman
TTImageView handles that sort of thing by itself, and should do so in this situation. reloadData isn't an option .. ever. Even without this problem, calling reloadData every time an image downloads (which could be a lot of images) will definitely mess up the table.
MiRAGe
A: 

I've written something similar to this where an image view will load its own image from the web.

Im my experience, when the image had loaded successfully but was not shown in its view, it was a case that the cell needed to be told to redraw.

When you scroll the table view, the cells are set to redraw when the come onscreen, which is why they appear when you scroll.

When the image loads, tell the cell that it is sitting in to redraw by sending it the message setNeedsDisplay.

That way, when the image finishes downloading, the cell its sitting in (and only that cell) will redraw itself to show the new image.

It's possible that you might not need to redraw the entire cell and might be able to get away with simply redrawing the image view using the same method call. In my experience, my table cells view hierarchy was flattened, so I had to redraw the whole cell.

Jasarien
+2  A: 

Sounds like a threading problem to me, are you creating TTImageView in the main runloop?

Deniz Mert Edincik
A: 

I find one interesting thing. When I use combination TTTableViewController, TTTableViewDataSource and TTModel I have same problem with loading TTImageView. My problem was, that my implementation of Model methods 'isLoading' and 'isLoaded' don't return proper values after initialization of model. That forces me to call reload on model manualy in 'viewDidAppear' method and that causes image loading problem. So I repair my 'isLoading' and 'isLoaded' methods to both return 'NO' after Model init, and everything is fine.

mivasi
A: 

I don't have an answer for what you want to do, but I will say that this is considered a feature, and the expected behavior. You use TTImageView in UITableView when you want to do lazy loading of images. TTImageView will only load the images whose frames are visible on the screen. That way, the device uses its network resources to download images that the user has in front of them, rather than a bunch of images that the user isn't even trying to look at.

Consider having a long list that may contain a couple hundred thumbnail images (like a list of friends). I know from experience that if you kick off 100+ image requests on older devices, the memory usage will go through the roof, and your app will likely crash. TTImageView solves this problem.

Jared Egan
I get the lazy loading principle, but it was a threading problem. NSURLRequest can only perform on the main thread.
MiRAGe