0

我正在异步下载表格中的图像。当表格第一次出现时,不会调用 connecion:didrecieveData 但是当我向上或向下移动时,它会被调用并显示图像。好心的帮助

表视图类:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    customiseForVideos *cell =(customiseForVideos *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"customiseForVideos" owner:self options:nil];
        cell=tblCell;
    }
    else{
        AsyncImageView* oldImage = (AsyncImageView*)
        [cell.contentView viewWithTag:999];
        [oldImage removeFromSuperview];

    }
    [cell.imageActivity startAnimating];

    CGRect frame;
    frame.size.width=65; frame.size.height=70;
    frame.origin.x=24; frame.origin.y=12;
    AsyncImageView* asyncImage = [[[AsyncImageView alloc]
                                   initWithFrame:frame] autorelease];
    asyncImage.tag = 999;
    NSURL* url = [NSURL URLWithString:[[videoCollection objectAtIndex:indexPath.row] videoImageUrl]];
        //NSLog(@"%@",url);
    [asyncImage loadImageFromURL:url activity:cell.imageActivity];

    [cell.contentView addSubview:asyncImage];

    if((indexPath.row%2)==0)
        cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green-cell-row.png"]] autorelease];
    else{
        cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue-cell-row.png"]] autorelease];
    }
    return cell;
}

AsyncImageView 类:

- (void)loadImageFromURL:(NSURL*)url activity:(UIActivityIndicatorView *)cellActivity {
    activityOnCell=cellActivity;
    activityOnCell.hidden=NO;
    if (self.connection!=nil) { [self.connection release]; } //in case we are downloading a 2nd image
    if (data!=nil) { [data release]; }

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object
    NSLog(@"%@",request);
        //TODO error handling, what if connection is nil?
}


//the URL connection calls this repeatedly as data arrives
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; } 
    [data appendData:incrementalData];
}

//the URL connection calls this once all the data has downloaded
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
    //so self data now has the complete image 
    [self.connection release];
    self.connection=nil;
    if ([[self subviews] count]>0) {
        //then this must be another image, the old one is still in subviews
        [[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
    }

    //make an image view for the image
    UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
    //make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
    [self addSubview:imageView];
    imageView.frame = self.bounds;
    [imageView setNeedsLayout];
    [self setNeedsLayout];
    activityOnCell.hidden=YES;

    [data release]; //don't need this any more, its in the UIImageView now
    data=nil;
}
4

4 回答 4

2

将图像加载到 TableView 中是一个常见问题。如此之多,以至于解决方案有所下降。您最好使用其中之一,然后尝试设计自己的。您将节省大量时间,通常会找到优化的解决方案,如果您需要更多功能,您可以随时进行子类化。

每次出现新图像时重新加载表格并不是最好的解决方案。当您重新加载时,如果用户正在滚动,他会失去他的位置。

使用https://github.com/AFNetworking/AFNetworking

这是做你需要的事情是多么容易。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];

[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
于 2012-05-22T07:26:27.950 回答
1

收到图像后,您不会重新加载 tableview。采用

[mtableView reloadData]; 

在连接完成加载。如果它不是tableview所在的同一个类,那么创建另一个方法并放置重新加载表并通过委托从其他类调用它。

于 2012-05-22T07:22:53.290 回答
0

有一个很棒的 3rd 方包,可以在一行代码中为您完成所有工作: https ://github.com/rs/SDWebImage 。

您需要做的就是导入此类别 ( #import "UIImageView+WebCache.h")

然后- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{} 你应该用一种简单的方法设置单元格的图像:

    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"noPic.png"]];

你完成了:)

于 2012-05-22T07:30:38.820 回答
0

使用 NSCache 临时存储图片,使用 url 作为 key,使用 NSData 作为 value。

并在 cellforRowAtIndexPath 方法中从 NSCache 对象获取值并将其分配给 imageview。它会停止闪烁并可见。

于 2012-05-22T07:50:52.650 回答