1

我有 UIScrollView 包含来自服务器的图像。

我应该在图像当前加载时放置活动指示器。

UIScrollView 包含来自服务器的动态数量的图像。

我可以知道如何在加载图像时在每个页面上添加活动指示器,并在加载图像后删除。

这是我检索图像的代码:

NSDictionary *items = [NSDictionary dictionaryWithObject:dictInfo forKey:@"images"];
imageList = [items objectForKey:@"images"];
NSArray *img = [imageList objectForKey:@"list"];
NSInteger imgCount = [img count];
buttonArray = [[NSMutableArray alloc] init];

for (int i=0; i<imgCount; i++) {
    NSDictionary *imgDict = [img objectAtIndex:i];
    // REQUEST FOR IMAGES
    NSString *imgPath = [imgDict objectForKey:@"image_slot"];
    NSString *imgURL = imgPath;

    __block ASIHTTPRequest *requestImage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imgURL]];
    [requestImage setCompletionBlock:^{
        imgView = [UIImage imageWithData:[requestImage responseData]];

        if (imgURL.length) {
            [pendingRequests removeObjectForKey:imgURL];
        }

        scrollView.userInteractionEnabled = YES;
        scrollView.exclusiveTouch = YES;
        scrollView.canCancelContentTouches = YES;
        scrollView.delaysContentTouches = YES;
        scrollView.bounces = NO;

        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        SWTUIButton *imgBtn = [[SWTUIButton alloc] initWithFrame:frame];
        imgBtn.url = [requestImage.userInfo objectForKey:@"rURL"];
        [imgBtn setImage:imgView forState:UIControlStateNormal];
        imgBtn.backgroundColor = [UIColor clearColor];
        [imgBtn addTarget:self action:@selector(buttonpushed:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:imgBtn];

        [buttonArray addObject:imgBtn];

        [imgBtn release];

        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * img.count, self.scrollView.frame.size.height);
    }];
4

1 回答 1

1

我强烈建议您使用https://github.com/jverkoey/nimbus项目中的 NINetworkImageView。它非常轻巧且有用。它有一个委托方法,让您知道何时加载图像。您基本上需要做的是: 1. 为每个页面创建一个 NINetworkImageView,就像您使用 UIImageView 一样,并调用 set

  NINetworkImageView* networkImageView = [[[NINetworkImageView alloc] initWithImage:initialImage]
                                      autorelease];
  networkImageView.delegate = self;
  networkImageView.contentMode = UIViewContentModeCenter;
[networkImageView setPathToNetworkImage:
     @"http://farm3.static.flickr.com/2484/3929945380_deef6f4962_z.jpg"
                             forDisplaySize: CGSizeMake(kImageDimensions, kImageDimensions)];

https://github.com/jverkoey/nimbus/tree/master/examples/photos/NetworkPhotoAlbums

  1. 将指标作为子视图添加到 networkImageView。
  2. 实现委托如下:

    -(void)networkImageView:(NINetworkImageView *)imageView didLoadImage:(UIImage *)image { [imageView removeAllSubviews];

    }

最终结果将是一个更小的代码来做同样的事情。

于 2012-04-28T05:54:19.410 回答