Free memory used by CGContextDrawPDFPage

When I analyze my application using tools, I found out that the data allocated by CGContextDrawPDFPage will not be sent immediately. Since my program receives many β€œmemory warnings”, I would like to free as much memory as possible, but I do not know how to free this memory.

As you can see at http://twitpic.com/473e89/full , this looks like 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 already tried to wrap AutoReleasePool around it, but this does not seem to have any effect. A screenshot is taken after the TiledView (the view the method belongs to) is released.

I hope someone can help me reduce memory usage.

+4
source share
2 answers

Everyone I know who was dealing with PDF files on iOS at some point encountered the same problem. The only solution seems to be to release the document and recreate it.

Please note that another common problem is that when you release your document, CATiledLayer may simultaneously display the page from that document. Thus, you should make good use of @synchronize (possibly using your pdfDocRef) and only release the document when the tile layer has completed its work.

Also check this out: Fast and fast PDF viewer for iPhone / iPad / iOs - tips and tricks?

+3
source

I had a similar problem,

this line that gets the pdf page

 [self.superview.superview getPage] 

make sure you reopen pdf every time you pull out the page, it turns out that CGPDFDocument does a great job of memory.

eg. sort of

 //release the current pdf doc if(self.pdfDocumentRef!=nil){ CGPDFDocumentRelease(self.pdfDocumentRef); } //open it again self.pdfDocumentRef=CGPDFDocumentCreateWithURL(..your url..); //pull out a page CGPDFPageRef pg = CGPDFDocumentGetPage(self.pdfDocumentRef, pageIndex+1); 
+1
source

Source: https://habr.com/ru/post/1342624/


All Articles