2

Hi iam developing an app where i will fetch images from web service and load the images in tableview. I loaded the images asynchronously. The problem is my app get crash while scrolling the tableview and in log it shows memory recieved warning.Also same images gets repeated in many rows.Also it takes more time to load. i used the below code.

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

{ static 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;

}

In view did load

mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

In .h file

dispatch_queue_t mainQueue;

Please help to load the images properly without any memory warning(Crash).Thanks in Advance.

Uma rajendran
  • 287
  • 1
  • 4
  • 18

1 Answers1

0

you are reusing tableviewcells. When a cell moves off the screen, it will be set aside so that you can reuse the object. When you are doing a dequeueReusableCellWithIdentifier you can get a 'old' cell that already contains an image. If you dont' clear the data from that cell you will see the old data (image) until the new image is downloaded.

inside the if(cell==nil) you should only create the cell and set properties that will be the same for every row. Set and clear the data below that 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

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58