2
votes

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???

1

1 Answers

2
votes

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);