I am using Path's excellent FastImageCache library and am running into a retain problem.
Specifically, when trying to provide UIImages (downloaded from a server) to the image cache. The images, when handed to the completion block, seems to remain retained. ARC is not releasing is.
When dealing with a very large number of large images, this leads to a dramatic memory growth.
- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id<FICEntity>)entity withFormatName:(NSString*)formatName completionBlock:(FICImageRequestCompletionBlock)completionBlock
{
...
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
UIImage* image = [[UIImage alloc] initWithData:data];
completionBlock(image); // <-----
...
}
I verified that not calling the completion block with the image, does not lead to the memory growth.
Also, not using the image in the drawing block itself, also avoids the memory growth. So i assume this is indeed related to the image being retained.
- (FICEntityImageDrawingBlock)drawingBlockForImage:(UIImage*)image withFormatName:(NSString*)formatName
{
FICEntityImageDrawingBlock drawingBlock = ^(CGContextRef contextRef, CGSize contextSize) {
CGRect contextBounds = CGRectZero;
contextBounds.size = contextSize;
CGContextClearRect(contextRef, contextBounds);
CGContextSetInterpolationQuality(contextRef, kCGInterpolationMedium);
UIGraphicsPushContext(contextRef);
[image drawInRect:contextBounds]; // <----
UIGraphicsPopContext();
};
return drawingBlock;
}
How can i avoid the image being retained or how can i force it to be released? I tried setting it to nil and adding a @autoreleasepool but that didn't help.
@autoreleasepool {
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
UIImage* image = [[UIImage alloc] initWithData:data];
completionBlock(image);
image = nil;
data = nil;
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
Thanks for the help.
UPDATE 3/18/2014
Note: this does NOT appear to be an issue in the simulator, only on the device.