tags:

views:

98

answers:

1

I've been struggling for ages trying to make a working PDF reader for iPhone/iPad, but the thing just won't stop eating memory. It seems that all the pages drawn with CGContextDrawPDFPage are cached internally and never released.

I'm not the only one with this problem: http://lists.apple.com/archives/quartz-dev/2010/Apr/msg00025.html

http://web.archiveorange.com/archive/v/SEb6acvPicMB2NdIjEd3

One response from the first thread:

If this is what I think it is, it isn't a leak but rather a cache. Instead of retaining the PDF document between calls, try creating the document, getting and drawing the page, then releasing the document when your done.

I've tried it, and it solves the problem, but it's too damn slow. Whereas doing it in the "normal" way (i.e. without loading/releasing CGPDFDocument for every draw) works good and reasonably fast until I reach the 4th or 5th page of the document and it crashes.

I've googled this problem for ages: everybody has the same problem but apparently nobody got it working. The only samples I found load a one-page PDF, so obviously everything works fine until you load a document with multiple pages (and of course if the thing has images in it, it crashes even earlier). This is driving me crazy.

So the question is: how exactly do you handle PDF drawing with CGPDF in your app? Any help would be greatly appreciated. Thanks.

+1  A: 

My current Enterprise Project is based round a PDF reader. I don't release CGPDFDocument and I can page through a 14 page apple pdf with the app maxing at 30mb. I cant give you code but i can point you in the right direction.

I started a similar thread here: http://stackoverflow.com/questions/3889634/fast-and-lean-pdf-viewer-how-does-ibooks-do-it

Some tips:

-Save any media to disk when you can.

-Use larger tileSizes if rendering on TiledLayers

-Note that images will render faster than a CGPDFPageRef

-Use NSOperations to prepare pages ahead
of time.

-Recycle page objects

-Close any open Contexts as soon as you don't need them

-on receiving a memory warnings release and reload the DocRef

Luke Mcneice
Thanks for answering and linking to that thread, helped a lot.
Brainfeeder