lazy loading images in UIScrollView
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:curre开发者_运维知识库ntImageToLoad];
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];
Why is the code in the willbegindecelerating method? It would be better to have it in the scrolviewdidscroll method so loading can begin asap.
You should not modify any views from a separate thread as view drawing is not thread safe
Are the images really stored as data in a file or are they image files? If image files, use [UIImage imageNamed:] to load them as you are performing 2 conversions at the moment. If they are data, I suggest you create UIImages for most of them on a separate thread when the scollview is first shown, so that they images are ready when they need to be added to the scrollview.
精彩评论