uitableviewcell内的异步加载图像

时间:2013-01-22 01:24:35

标签: objective-c asynchronous uitableview loading-image

您好我正在尝试在CustomTableViewCell中使用EGOImageView来定制单元格。这是我使用EGOImageView的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* simpleTableIdentifier = @"Albums";

CustomTableCell* cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (!cell)
{
   cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    NSLog(@"Show once or more times");
}

NSDictionary* dictionary = (NSDictionary*)[self.albumCollection objectAtIndex:indexPath.row];

cell.label.text = [dictionary valueForKey:@"name"];

EGOImageView* imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageWithContentsOfFile:@""]];
[imageView setImageURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small&access_token=%@", (NSString*)[dictionary valueForKey:@"id"], [[FBSession activeSession] accessToken]]]];
[imageView setFrame:CGRectMake(0.0f,0.0f,78.0f,78.0f )];


[cell.iView addSubview:imageView];
[imageView release];

每个单元格上的图像加载相同的图像。是不是因为它在加载图像时重用了单元格。

我发现了一个问题,我无法想到问题发生的原因。我使用图形api来抓取图像https://graph.facebook.com/%@/picture?type=small&access_token=%@,其中第一个参数是专辑ID。

为了让自己很容易看到问题,我只在单元格上使用了一个专辑,无论我使用的是哪张相册都出现了相同的照片。但是当我将链接复制到浏览器时,地址栏上显示的实际照片网址显示的是图片,并显示正确的照片。

有谁知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

这是一个例子。它从后台的某个服务器加载用户图片并更新单元格图像。请注意,imageView.image在开头设置为nil。这是细胞重用的情况,因此在下载时您将没有图像而不是错误的图像。

要添加的另一件事是,拥有缓存也不错,因此它不会一直下​​载图像。另一件好事是不在边缘网络中下载图像。

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TransactionCell";
        NSMutableArray *data = searching ? searchResult : dataSource;
        NSDictionary *object = [data objectAtIndex:[indexPath row]];

        UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithIdentifier:CellIdentifier] autorelease];
        }
        cell.imageView.image = nil;
        cell.textLabel.text = @"Your cell text";
        NSString *contact = @"foo@gmail.com";
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                        NSData *imgData = [appDelegate addUserPic:contact];
                        if (imgData == nil && netStatus == ReachableViaWiFi) {
                                NSString *url = [NSString stringWithFormat:@"http://somehost.com/userpic/%@", contact];
                                imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
                        }
                        dispatch_async(dispatch_get_main_queue(), ^{
                                UITableViewCell *updateCell = [self.tableView cellForRowAtIndexPath:indexPath];
                                if (updateCell) {
                                        if (imgData) {
                                                [appDelegate setUserPic:contact imgData:imgData];
                                                updateCell.imageView.image = [UIImage imageWithData:imgData];
                                        } else {
                                                updateCell.imageView.image = nil;
                                        }
                                        /* This forces the cell to show image as now
                                           it has normal bounds */
                                        [updateCell setNeedsLayout];
                                }
                        });
                });
        return cell;
}
相关问题