我有一個應用程序需要滾動許多圖像。在UIScrollView中延遲加載圖像
因爲我無法加載所有的圖像,並將其保存在內存中,我懶加載它們。
問題是如果我滾動得太快,顯示「黑色圖像」(圖像,無法加載)。
這是延遲加載代碼: 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];
這裏面scrollViewWillBeginDecelerating代碼:
[NSThread detachNewThreadSelector:@selector(lazyLoadImages) toTarget:self withObject:nil];