0

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

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

我了解到延迟加载是实现此目的的一种方法。但是,我并不完全了解它是如何完成的。

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

任何预兆都可以帮助我举一个简单的例子,这将帮助我从头开始理解吗?并会为我指明正确的方向。

感谢您的时间。

4

3 回答 3

1

好吧,不需要将图像存储到核心数据中。只需将所有图像 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];
               }
          }
     }
}

让我知道这是否对您有帮助。在此先感谢。一切顺利。

于 2013-10-30T06:15:15.713 回答
0

利用滚动视图的委托函数、内容大小和内容插入属性

于 2013-10-30T05:25:36.403 回答
0

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

于 2013-10-29T18:01:27.880 回答