简体   繁体   English

使用延迟加载在scrollview上加载图像

[英]Loading images on scrollview using lazy loading

I am working on an app in which I need to load nearly 211 images in a scrollview. 我正在开发一个需要在滚动视图中加载近211张图像的应用程序。 What I was doing was I was converting the images in binary format (NSData) and saving them in core data. 我正在做的是将图像转换为二进制格式(NSData)并将其保存在核心数据中。 Then retrieving them and populating them on the scrollview. 然后检索它们并将其填充在scrollview中。

it does work, but sometimes it does throw "Memory warning". 它确实可以工作,但是有时确实会引发“内存警告”。

I learned that lazy loading is a way to achieve this. 我了解到,延迟加载是实现此目标的一种方法。 But, I am not totally aware of how it is done. 但是,我并不完全知道它是如何完成的。

Like, loading 3 or 5 images ahead/back of the image visible currently on the scrollview. 例如,在滚动视图上当前可见的图像之前/之后加载3或5个图像。

Can any bode help me with a simple example, which would help me understand from scratch ? 有什么预兆可以帮助我举一个简单的例子,这将使我从头开始理解吗? and would point me in the right direction. 会指出正确的方向

Thank you for your time. 感谢您的时间。

Well, it is not needed to store images into core data. 好吧,不需要将图像存储到核心数据中。 Just take all image URLs into NSMutableArray and try to use following code as per your requirement.Hope this helps you. 只需将所有图像URL放入NSMutableArray中,然后根据需要尝试使用以下代码,希望对您有所帮助。

Try this code - 试试这个代码-

    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;

    }

Here meetUPFrndPhotoArray contains image URLs. 这里的meetUPFrndPhotoArray包含图像URL。

And

For Downloading images in Background - 用于在后台下载图像-

- (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];
  }

For Assigning downloaded image to perticular imageView - 用于将下载的图像分配给垂直的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];
               }
          }
     }
}

Let me know if this helps you.Thanks in advance.All the best. 让我知道这是否对您有帮助。

签出该这是一个通用的UIScrollView,可以像UITableView一样重用其子视图(有一个dataSource对象,用于配置滚动视图子视图)

利用scrollview的委托函数,内容大小和内容插入属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM