使用延迟加载在滚动视图上加载图像

我正在一个应用程序中,我需要在滚动视图中加载近211个图像。 我正在做的是我将图像转换为二进制格式(NSData)并将其保存在核心数据中。 然后检索它们并在scrollview上填充它们。

它确实有效,但是有时它会抛出“内存警告”。

我了解到,懒加载是实现这一目标的一种方法。 但是,我并不完全知道它是如何完成的。

就像在当前滚动视图中可见的图像的前面/后面加载3或5个图像一样。

任何波特能帮助我一个简单的例子,这将帮助我从头开始理解? 并指出我在正确的方向。

感谢您的时间。

那么,不需要将图像存储到核心数据中。 只要把所有的图像url到NSMutableArray,并尝试按照您的要求使用下面的代码。希望这可以帮助你。

试试这个代码 –

friendsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 470, 320, 100)]; friendsScrollView.contentSize = CGSizeMake([meetUPFrndNameArray count] * 95,50); friendsScrollView.backgroundColor = [UIColor clearColor]; friendsScrollView.delegate = self; [self.view addSubview:friendsScrollView]; int imageX = 25,imageY = 20; int backImageX = 10, backImageY = 10; int flag = 0; for (int i = 0; i < [meetUPFrndPhotoArray count]; i++) { flag ++; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = paths[0]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[flag-1]]; UIImage *img1 = [UIImage imageWithContentsOfFile:savedImagePath]; if (!img1 || [UIImagePNGRepresentation(img1) length] <=0) { id path = meetUPFrndPhotoArray[flag-1]; path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; NSURL *url = [NSURL URLWithString:path]; NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:url, [NSString stringWithFormat:@"%d", flag], nil]; [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr]; UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)]; frndBackgroundImage.image = [UIImage imageNamed:@"friend_image_background.png"]; frndBackgroundImage.backgroundColor = [UIColor clearColor]; frndBackgroundImage.layer.borderWidth = 2.0; frndBackgroundImage.layer.masksToBounds = YES; frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1); frndBackgroundImage.layer.shadowOpacity = 1; frndBackgroundImage.layer.shadowRadius = 1.2; frndBackgroundImage.layer.cornerRadius = 5.0; frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor]; [friendsScrollView addSubview:frndBackgroundImage]; UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)]; friendImage.tag = flag; friendImage.image = [UIImage imageNamed:@"ic_user.png"]; [friendsScrollView addSubview:friendImage]; } else { UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)]; frndBackgroundImage.image = [UIImage imageNamed:@"friend_image_background.png"]; frndBackgroundImage.backgroundColor = [UIColor clearColor]; frndBackgroundImage.layer.borderWidth = 2.0; frndBackgroundImage.layer.masksToBounds = YES; frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1); frndBackgroundImage.layer.shadowOpacity = 1; frndBackgroundImage.layer.shadowRadius = 1.2; frndBackgroundImage.layer.cornerRadius = 5.0; frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor]; [friendsScrollView addSubview:frndBackgroundImage]; UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)]; friendImage.tag = flag-1; friendImage.image = img1; [friendsScrollView addSubview:friendImage]; } backImageX = backImageX + 80 + 10; backImageY = 10; imageX = imageX + 50 + 25 + 15; imageY = 20; } 

这里meetUPFrndPhotoArray包含图像的URL。

在后台下载图像 –

 - (void) loadImageInBackground:(NSArray *)urlAndTagReference{ NSData *imgData = [NSData dataWithContentsOfURL:urlAndTagReference[0]]; UIImage *img = [[UIImage alloc] initWithData:imgData]; NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:img, urlAndTagReference[1], nil]; [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES]; } 

将下载的图像分配给特定的imageView –

 - (void) assignImageToImageView:(NSArray *)imgAndTagReference{ for (UIImageView *checkView in [friendsScrollView subviews] ){ if ([imgAndTagReference count] != 0) { if ([checkView tag] == [imgAndTagReference[1] intValue]){ [checkView setImage:imgAndTagReference[0]]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = paths[0]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[[imgAndTagReference[1] intValue]-1]]; UIImage* imageToSave = [checkView image]; NSData *imageData = UIImagePNGRepresentation(imageToSave); [imageData writeToFile:savedImagePath atomically:NO]; } } } } 

让我知道这是否有助于你。感谢提前。一切顺利。

利用滚动视图的委托function,内容大小和内容embedded属性

检出这个库它是一个通用的UIScrollView,像UITableView一样重用它的子视图(有一个dataSource对象,用于configuration滚动视图子视图)