0

Simple scenario: table view controller, with 5.000 rows. I have 5000 images name from 0.png to 4999.png.

Here is my cellForRowAtIndexPath: method :

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *string = [NSString stringWithFormat:@"%d",indexPath.row];
UIImage *img = [UIImage imageNamed:string];

if (img) {
    cell.imageView.image  = img;
} else {
    cell.imageView.image = [UIImage imageNamed:@"NoPhotoDefault"];
}   
return cell;

I've analized the behaviour whit instruments, and it indicates that real memory usage is rising fast at scrolling and it never goes down.

It start from about 8 MB and it goes up to 200 MB when reaching last row.

What am i missing here?

Thanks

P.S. Proj base sdk is 5.0, and using ARC.

Later edit, when sending the app in background, memory is freed.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
AndreiS
  • 261
  • 3
  • 12

1 Answers1

1

UIImage caches the images when you use the method imageNamed:. It does to avoid reloading the same image when it is used multiple times.

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

If you want to avoid caching the images, use imageWithContentsOfFile: instead.

This method does not cache the image object.

sch
  • 27,436
  • 3
  • 68
  • 83