使用AsyncImageView类显示延迟加载的活动指示器

时间:2013-07-22 05:21:56

标签: iphone ios6

我正在使用AsyncImageView类在UITableView上应用延迟加载。并希望在图像视图上应用活动指示器,直到图像加载到单元格上。以下是我正在尝试的代码。

//  AsyncIamgeView.m

 - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

//[connection release];
UIActivityIndicatorView     *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.center = CGPointMake(15, 15);

connection=nil;

if ([[self subviews] count]>0) {
    [[[self subviews] objectAtIndex:0] removeFromSuperview];
}

UIImage *imgData = [UIImage imageWithData:data];
UIImageView* imageView = [[UIImageView alloc]init];
 [imageView addSubview:indicator];
[indicator startAnimating];

    if(imgData == nil)
{
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NoImagenew.png"]];
    //[indicator stopAnimating];
}
else{
    imageView = [[UIImageView alloc] initWithImage:imgData];
   // [indicator stopAnimating];
}
//imageView.contentMode = UIViewContentModeScaleAspectFit;
//imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
//[imageView sizeToFit];
[self addSubview:imageView];

imageView.frame = self.bounds;
//imageView.frame = CGRectMake(0, 0, 85, 94);
[imageView setNeedsLayout];
[self setNeedsLayout];
//[data release];
data=nil;
}

 //   cellForRowAtIndexPath method.

 asyncImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(1, 3, 85, 54)];
[asyncImageView loadImageFromURL:[NSURL URLWithString:imageUrlString]];
[cell.contentView addSubview:asyncImageView];

此代码显示活动指示器,但是在加载图像之后加载图像时。请指导以上。

5 个答案:

答案 0 :(得分:1)

你必须创建AsyncImageView对象而不是UIImageView,然后它会自动为你的视图添加指标

AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(1, 3, 85, 54)];
[cell addSubview:imageView];


//cancel loading previous image for cell
    [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];

    //load the image
    imageView.imageURL = [imageURLs objectAtIndex:indexPath.row];

以下是此

example

答案 1 :(得分:0)

目前,您正在下载图片时添加活动指示器。

这是一个简单的想法,希望你能在代码中实现这个

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   // This method is called when your connection receives a response
   // add your activity indication here and start animating
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  // This method is called when your image is downloaded.
  // remove your activity indicator or stop animating here
}

答案 2 :(得分:0)

我认为您正在使用asyncimageview类,默认情况下,您将在单元格中获取加载器。

答案 3 :(得分:0)

希望您从https://github.com/nicklockwood/AsyncImageView下载AsyncImageView。默认情况下,它具有显示活动指示器的功能。您只需自己添加或删除任何代码即可使此功能正常工作。只需致电loadImageWithURL

答案 4 :(得分:0)

AysncImageView已经有了一个加载器。

如果你的背景为黑色,你就不会看到它,因为它加载了默认活动指示器。

因此,只需根据背景为AsyncImageView对象设置 activityIndi​​catorStyle 属性。

就我而言,我的背景是黑色的,所以我使用了以下代码:

asyncImgView.activityIndicatorStyle = UIActivityIndicatorViewStyleWhite;