Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deallocate memory used by CGContextDrawPDFPage

When I analyze my app with Instruments, I found out that data allocated by CGContextDrawPDFPage is not released immediately. Since my program receives a lot of 'memory warnings' I would like to release as much memory as possible but I don't know how to release this memory.

As you can see on http://twitpic.com/473e89/full it seems to be related to this code

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx{
    NSAutoreleasePool * tiledViewPool = [[NSAutoreleasePool alloc] init];
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform([self.superview.superview getPage],kCGPDFMediaBox,tiledLayer.bounds, 0, true);
    CGContextSaveGState (ctx);
    CGContextTranslateCTM(ctx, 0.0, tiledLayer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    CGContextConcatCTM (ctx, pdfTransform);
    CGContextClipToRect (ctx, CGPDFPageGetBoxRect([self.superview.superview getPage],kCGPDFMediaBox));
    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
    CGContextDrawPDFPage(ctx,[self.superview.superview getPage]);
    CGContextRestoreGState (ctx);
    UIGraphicsEndPDFContext();
    [tiledViewPool drain];
}

I have already tried to wrap an AutoReleasePool around it but this does not seem to have any influence. The screenshot is taken after TiledView (the view where the method belongs to) is deallocated.

I hope someone can help me to reduce memory usage.

like image 321
Arie Timmerman Avatar asked Mar 07 '11 11:03

Arie Timmerman


1 Answers

Everyone I know who had to deal with PDFs on iOS at some point has had this same problem. The only solution seems to be to release the document and re-create it.

Note that the other common problem is that while you're releasing your document, a CATiledLayer may be rendering a page from that document at the same time. So, you should make good use of @synchronize (probably using your pdfDocRef) and release the document only when the tiled layer has finished its work.

Also, check this out: Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

like image 141
ySgPjx Avatar answered Oct 13 '22 01:10

ySgPjx