Mithuzz
Mithuzz

Reputation: 1091

Asynchronous image loading in UITableViewCell memory issue

In my iPhone application I have a tableview with custom imageview and loading images from remote location using AsyncImageView class. It works nicely, but one issue is, if I scroll the table, cells will be dequeued and it again trying to get the images from server. So, the method for loading image from AsyncImageView class is calling again and again hence increases the memory allocation, and eventually the app crashes.

Here is my code:

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 300, 40);

    CGRect userImageFrame = CGRectMake(5, 7, 36, 36);

    UIImageView *userImage;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    [cell setFrame:CellFrame];

    userImage = [[UIImageView alloc]init];
    userImage.frame = userImageFrame;
    userImage.tag = 3;
    [cell.contentView addSubview:userImage];
    [userImage release];

    return cell;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
        cell = [self getCellContentView:CellIdentifier];
    else
        [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imageView];

    UIImageView *userImage = (UIImageView *)[cell viewWithTag:3];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    NSString *url = [[NSString alloc]initWithFormat:@"%@%@", CommonImageURL,[AllUsersProfileImageArray objectAtIndex:indexPath.row]];
    NSURL *imageUrl =  [NSURL URLWithString:[url stringByAppendingFormat:@"?%i", rand()]];
    [url release];

    userImage.image = [UIImage imageNamed:@"defaultPerson.png"];
    userImage.imageURL = imageUrl;
    return cell;
}

Is there any possible way to fix the issue? Please help.

Upvotes: 1

Views: 1545

Answers (4)

Simon
Simon

Reputation: 791

I posted a custom solution here Download image asynchronously .

I think it works ok and requires very little code.

Upvotes: 0

Mike
Mike

Reputation: 1219

I came across same trouble of memory leaks loading multiple images from server. My application needed fresh images response every time (functionality factor)
  I was using NSURLConnection using asynch requests. I tried with

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:0];
[NSURLCache setSharedURLCache:sharedCache]; // in DidFinishLoading & didFailWithError

receivedData=nil;  // at didReceiveResponse
receivedData=[[NSMutableData alloc] init];

but nothing really helped , until I removed my cell [cell removeFromSuperview]; in cellForRowAtIndexPath and initialized it again FOR every new NSURL request (additional if condition on cellForRowAtIndexPath but REALLY that payed off).

Probably the UIImageViews were never removed and new images n data were added constantly as fetched by responses. Removing old UITableViewCell for a new NSURLRequest worked in my case.
  Hope this helps someone like me lost in cleaning NSURLCache and still memory beefing up.

Upvotes: 0

IngmarStein
IngmarStein

Reputation: 668

The popular AFNetworking library also includes a UIImageView category to load images from the web which is often overlooked. I found it to be quite efficient with respect to memory usage and easy to use.

http://afnetworking.github.com/AFNetworking/Categories/UIImageView+AFNetworking.html

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107121

The best solution will be caching the image that is already downloaded and displaying it from there.

You need to write code for that, or there are some libraries which provide such feature:

  1. HJCache
  2. SDWebImage

Upvotes: 2

Related Questions