0

我正在使用 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];

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

4

5 回答 5

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

这是一个例子

于 2013-07-22T11:33:24.890 回答
0

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

于 2013-07-22T07:11:50.103 回答
0

AysncImageView 已经有一个加载器。

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

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

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

asyncImgView.activityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
于 2015-02-18T05:05:02.747 回答
0

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

于 2013-07-22T08:26:21.730 回答
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
}
于 2013-07-22T06:07:27.373 回答