2

嗨,我正在开发一个应用程序,我将从 Web 服务中获取图像并将图像加载到 tableview 中。我异步加载图像。问题是我的应用程序在滚动表格视图时崩溃,并在日志中显示内存收到警告。同样的图像在许多行中重复出现。加载需要更多时间。我使用了下面的代码。

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

{ 静态 NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    /*   UILabel * cellLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 50, cell.frame.size.width-20, 45)];
     cellLabel.textColor=[UIColor blackColor];
     cellLabel.backgroundColor=[UIColor clearColor];
     cellLabel.tag=2;
     [cell.contentView addSubview:cellLabel];*/

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_iPhone.png"]];


    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,18, 48, 48)];
    imv.tag=4;
    imv.image=[UIImage imageNamed:@"ImagePlaceholder.png"];


    [cell.contentView addSubview:imv];

    UIImageView *arrwimv = [[UIImageView alloc]initWithFrame:CGRectMake(260,35, 14, 17)];
    arrwimv.image=[UIImage imageNamed:@"arrw_iPhone.png"];

    [cell.contentView addSubview:arrwimv];



    UILabel *descriptionLbl=[[UILabel alloc] initWithFrame:CGRectMake(100, 27, 450, 45)];
    descriptionLbl.font=[UIFont CITY311_TitleFontWithSize:18];
    descriptionLbl.tag=1;
    descriptionLbl.textAlignment=UITextAlignmentLeft;
    descriptionLbl.textColor=[UIColor blackColor];
    descriptionLbl.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:descriptionLbl];

    UILabel *descriptionLbl2=[[UILabel alloc] initWithFrame:CGRectMake(100, 5, 450, 45)];
    descriptionLbl2.font=[UIFont CITY311_TitleFontWithSize:18];
    descriptionLbl2.tag=2;
    descriptionLbl2.textAlignment=UITextAlignmentLeft;
    descriptionLbl2.textColor=[UIColor blackColor];
    descriptionLbl2.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:descriptionLbl2];


}
UIImageView *imv2=(UIImageView *)[cell.contentView viewWithTag:4];

dispatch_async(mainQueue, ^(void) {

    if(![[[issueArray objectAtIndex:indexPath.row]objectForKey:@"PhotoUrl"] isEqualToString:@""])
    {

        NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[issueArray objectAtIndex:indexPath.row]objectForKey:@"PhotoUrl"]]];
        UIImage* image = [[UIImage alloc] initWithData:imageData];

        imv2.image = image;

    }


});


UILabel *lbl=(UILabel *)[cell.contentView viewWithTag:1];
lbl.text=[[issueArray objectAtIndex:indexPath.row] objectForKey:@"issueSubmittedDate"];


UILabel *lbl2=(UILabel *)[cell.contentView viewWithTag:2];
lbl2.text=[[issueArray objectAtIndex:indexPath.row] objectForKey:@"IssueName"];

return cell;

}

鉴于确实加载

mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

在 .h 文件中

dispatch_queue_t 主队列;

请帮助正确加载图像,而不会出现任何内存警告(崩溃)。在此先感谢。

4

1 回答 1

0

您正在重用 tableviewcells。当一个单元格移出屏幕时,它将被放在一边,以便您可以重复使用该对象。当您执行 dequeueReusableCellWithIdentifier 时,您可以获得一个已经包含图像的“旧”单元格。如果您不清除该单元格中的数据,您将看到旧数据(图像),直到下载新图像。

在 if(cell==nil) 内,您应该只创建单元格并设置每一行都相同的属性。设置并清除 if 下面的数据。

The crashes probably happen because a cell can be moved out of the view and reused for an other row before the initial callback is ready. Try setting the identifier to a unique value. Something like: NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.Row]; Only keep your code like that when the number of rows is low. Otherwise you could get memory problems.

Instead of trying to fix it yourself, you could try using something like https://github.com/rs/SDWebImage

于 2013-01-03T13:00:40.317 回答