ScrollView content async downloading problem

Posted by Newbee on Stack Overflow See other posts from Stack Overflow or by Newbee
Published on 2010-04-27T08:26:19Z Indexed on 2010/04/27 9:03 UTC
Read the original article Hit count: 323

Hi! I have UIScrollView with lots of UIImageView inside. In the loadView method I assign some temporary image for each of subview UIImageView images and starts several threads to async download images from internet. Each thread downloads and assign images as follows:

    NSData  *data   = [NSData dataWithContentsOfURL:URL];
    UIImage *img    = [UIImage imageWithData:data];
    img_view.image  = img;

Here is the problem - I expects picture will changed after each image downloaded by I can see only temporary images until all images will downloads. UIScrollView still interact while images downloads - I can scroll temporary images inside it and see scrollers and nothing blocks run loop, but downloaded images doesn't updates..

What I tried to do:

  1. Call sleep() in the download thread -- not helps.
  2. Call setNeedsDisplay for each ImageView inside ScrollView and for ScrollView -- not helps.

What's wrong ? Thanks.

Update. I tried some experiments with number of threads and number of images to download. Now I'm sure -- images redraws only when thread finished. For example - if I load 100 images with one thread -- picture updates one time after all images downloads. If I increase number of threads to 10 -- picture updates 10 times -- 10 images appears per update.

One more update. I fixed problem by staring new thread from the downloading threads each time one image downloaded and exit current thread (instead of download several images in one thread in the cycle and exit thread only when all downloaded). Obviously it's not a good solution and there must be right approach.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk