lazy loading images in UIScrollView

Posted by idober on Stack Overflow See other posts from Stack Overflow or by idober
Published on 2010-05-03T18:07:13Z Indexed on 2010/05/04 14:08 UTC
Read the original article Hit count: 442

Filed under:
|

I have an application the requires scrolling of many images.

because I can not load all the images, and save it in the memory, I am lazy loading them.

The problem is if I scroll too quickly, there are "black images" showing (images that did not manage to load).

this is the lazy loading code: int currentImageToLoad = [self calculateWhichImageShowing] + imageBufferZone; [(UIView*)[[theScrollView subviews] objectAtIndex:0]removeFromSuperview]; PictureObject *pic = (PictureObject *)[imageList objectAtIndex:currentImageToLoad];

    MyImageView *iv = [[MyImageView alloc] initWithFrame:CGRectMake(currentImageToLoad * 320.0f, 20.0f, 320.0f, 460)];
    NSData *imageData = [NSData dataWithContentsOfFile:[pic imageName]];
    [iv setupView:[UIImage imageWithData: imageData] :[pic imageDescription]];
    [theScrollView insertSubview:iv atIndex:5];
    [iv release];

this is the code inside scrollViewWillBeginDecelerating:

[NSThread detachNewThreadSelector:@selector(lazyLoadImages) toTarget:self withObject:nil];

© Stack Overflow or respective owner

Related posts about iphone

Related posts about lazy-loading