Activity Indicator while loading images inside UIScrollView

谁说胖子不能爱 提交于 2019-12-24 11:35:50

问题


I have UIScrollView that contains images from the server.

I should put Activity Indicator while the image is currently loading.

UIScrollView contains dynamic number of images from the server.

May I know how can I add activity indicators on each page while the image is loading and remove once image is loaded.

Here's my code to retrieve images:

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

回答1:


I highly suggest you use NINetworkImageView from https://github.com/jverkoey/nimbus project. It's very light and useful. It has a delegate method to let you know when an image is loaded. What you basically need to do is: 1. create an NINetworkImageView for each page, just like you do with UIImageView, and call 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. the add the indicator to the networkImageView as a subview.
  2. implement the delegate as follows:

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

    }

the end result would be a much smaller code for doing the same thing.



来源:https://stackoverflow.com/questions/10344215/activity-indicator-while-loading-images-inside-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!