lazy loading images in UIScrollView
- by idober
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];