2

I have checked out many question in SO, they suggested to release and then recreate the CGPDFDocumentRef again. And my final code is like this

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CFURLRef pdfURL = (CFURLRef)_pdfLocation;

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);

if (CGPDFDocumentIsEncrypted(pdf)) {
    CGPDFDocumentUnlockWithPassword(pdf, (char *)[PDF_PASSWORD UTF8String]);
}

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, _pageNumber);

CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
CGContextTranslateCTM(ctx, 0.0f, [layer bounds].size.height);

CGContextScaleCTM(ctx, 1.0f, -1.0f);

CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
CGContextScaleCTM(ctx, [layer bounds].size.width / mediaRect.size.width, [layer bounds].size.height / mediaRect.size.height);
CGContextTranslateCTM(ctx, -mediaRect.origin.x, -mediaRect.origin.y);
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);    

CGContextDrawPDFPage(ctx, page);


CGPDFDocumentRelease(pdf);

}

Is the code above the right way to recreate the CGPDFDocumentRef? Cause "sometimes" it cause a leak at this line CGContextDrawPDFPage(ctx, page); , it is occurred when I scroll about 10 pages. And follow this link Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?, I have tried release the CGPDFDocumentRef whenever memory warning is occurred, but the result is CGPDFDocumentRef did not release all of the cache, but only release recently page, so the memory still increase all the way up. I thought the bug was fixed? How to release a CGPDFDocumentRef completely???

Community
  • 1
  • 1
Lunayo
  • 538
  • 7
  • 32

1 Answers1

2

Dont use

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);

in drawLayer. Since every time this is called it creates a new document ref. Instead create a single document ref in ur view controller and use it everytime.

Use this line instead CGPDFPageRelease (page);

DivineDesert
  • 6,924
  • 1
  • 29
  • 61
  • I have tried, and it is because of that the CGContextDrawPDFPage(ctx, page); will keep save cache and make the memory larger and larger till memory warning and crash – Lunayo Nov 16 '11 at 11:20
  • What have u tried?? CGPDFDocumentRef should be created once.. It takes lots of memory – DivineDesert Nov 16 '11 at 11:22
  • put the CGPDFDocumentRef in view controller, and then passing to the view and rendering. but CGContextDrawPDFPage(ctx, page); keep showing leak in instrument and memory is increase overtime. and in this link http://stackoverflow.com/questions/3889634/fast-and-lean-pdf-viewer-for-iphone-ipad-ios-tips-and-hints/3890161#3890161, I was told to release and reinit every time to draw layer? – Lunayo Nov 16 '11 at 11:25
  • 2
    I checked it now.. With Instrument.. My code is not leaking. You need to release page instead of Document – DivineDesert Nov 16 '11 at 11:31
  • I render page each time I swipe the page. page is get from cgpdfdocument is that necessary to release? Have you tried calling the draw layer in looping? – Lunayo Nov 16 '11 at 11:35
  • I am not releasing it every time.. But if you say it leaks at that point then there is something to do with page n not document itself.. Since `CGPDFDocumentCreateWithURL` itself take some time – DivineDesert Nov 16 '11 at 11:39
  • but the page is take from here `CGPDFPageRef page = CGPDFDocumentGetPage(pdf, _pageNumber);` is there needed to release? – Lunayo Nov 16 '11 at 11:41
  • I dont think so. I never faced this issue. – DivineDesert Nov 16 '11 at 11:43