Search code examples
iphoneuiscrollviewuiimageviewlazy-loadingphotos

IPhone SDK: Problem with lazy loading scrollView pictures


My app involves a scrollView containing imageViews chosen by the user through a modified ELCImagePicker. The chosen pictures will typically be high quality photos at 5 MB+ and the user will usually choose at least a dozen pictures at a time. Currently, I am loading the photos as below:

   -(void)loadViewWithPage: (int)page
{
    if (page > 0 && page < [Album count]) {
        [scrollView addSubview:[Album objectAtIndex:page]];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth) / pageWidth) + 1;

    [self loadViewWithPage:page + 1];
}

Where Album is where the photos are stored as imageViews.

This works great when the user is not trying to break the app and scroll through the photos one at a time, but fails miserably when he/she tries to scroll through the entire selection. The pages are blank unless the user stops after each photo. I tried using scrollViewDidScroll ala PageControl sample, but since the photos are all huge the lag is very visible.

Is there any way of loading the photos smoothly?


Solution

  • I had a similar situation and dealt with it by creating a custom subclass of NSOperation which would load the images in a separate thread and then display them by calling a method on the main thread.

    While the image was loading I displayed a UIActivityView

    Hope that helps.