Reputation: 739
I have my iOS app fetching images one at a time and showing them on the screen (very much like a slideshow). I'm running into Low Memory situations. Looking at Instruments when running Allocations, I see that the memory keeps growing. The Call Tree shows a large amount of memory being used, but none of the lines are of my code and I'm not experienced enough in debugging these situations to know how to read it.
Here's the code that I'm fetching the images with:
- (void)fetchLargeImage:(void (^)(UIImage *))completion
{
// image doesn't exist yet, fetch it now
NSURL *resourceURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://path_to_image/%@", @"image_name.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:resourceURL];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) {
return image;
} success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
completion(image);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
// image wasn't loaded
}];
[operation start];
}
As the images are shown, I store the last 10 images because the user can swipe backwards to review previously shown images. In my cleanup method, I nil out the image on the image view and the imageview itself, and remove it from the array. So I'm not sure what is still holding onto it and causing the memory to continue to grow. What am I missing here?
Upvotes: 0
Views: 864
Reputation: 4931
AFNetworking's UIImageView category methods quietly store the images they retrieve in a shared AFImageCache object, which is a subclass of NSCache. This is great in terms of performance, but I imagine it could be contributing to your memory problem. You'll see this line is called after each successful AFImageRequestOperation:
[[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest];
The category methods don't seem to offer much control over this as far as I can tell. If you feel comfortable editing the code in UIImageView+AFNetworking.m, you might comment out the above line, or add this line to the af_sharedImageCache
class method to limit the size of the cache:
[_af_imageCache setCountLimit: 10]; // limits the number of cached images to 10
I haven't tested this with Instruments so I am not 100% sure it will solve your problem, but maybe it helps explain what's going on.
Upvotes: 1