0

I am using the UIImageView+AFNetworkingin order to download and display an image from a URL in a UITableView. Everything is working great and my question is around the caching elements & allocations that are implemented with this call.

In using instruments to track memory I see that as I scroll my largest memory allocation quickly becomes this VM: Foundation which appears as if it is caching the images I am downloading in some way.

enter image description here

Which is great for user when the view the same image but I never see it release the memory. (I have not been able to get it to a memory warning yet). I just want to make sure then when needed this allocation will get released when needed. Below is the stack track of those VM : Foundation

enter image description here

Is there any need for me to monitor this and release the memory for the cache when needed to ensure smooth scrolling? Or is there anything else I should be watching to ensure this is handled properly?

Here is how I am calling for the images in cellForRowAtIndexPath I am calling the setImageWithURLRequest. Any advice or improvements would be appreciated.

   NSString *imageURL = [NSString stringWithFormat:@"https://IMAGE-URL/%@",imageURL];
        __weak MainTableViewCell *weakCell = cell;
        NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:imageURL]];

        [cell.postImage setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

            MainTableViewCell *strongCell = weakCell;
            strongCell.postImage.image = image;

            [UIView animateWithDuration:.15 animations:^{
                strongCell.postImage.alpha = 1;
            }];

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

        }];
Chase S
  • 448
  • 3
  • 20

1 Answers1

3

You really shouldn't worry about clearing image cache as AFNetworking handle this for you automatically.

It stores images internally in NSCache that as the docs say :

incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. The system automatically carries out these policies if memory is needed by other applications. When invoked, these policies remove some items from the cache, minimizing its memory footprint.

However, during AFNetworking development there were some troubles with this automatic removal behaviour, so finally manual cache clearing was added when receiving UIApplicationDidReceiveMemoryWarningNotification. But this all happens internally and you shouldn't do it yourself.

Maxim Pavlov
  • 2,962
  • 1
  • 23
  • 33