使用CGContextDrawPDFPage绘制PDF页面异常泄漏
我已经检查了很多问题,他们建议释放然后再次重新创建 CGPDFDocumentRef。我的最终代码是这样的
- (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);
}
上面的代码是重新创建 CGPDFDocumentRef 的正确方法吗?导致“有时”会导致该行发生泄漏 CGContextDrawPDFPage(ctx, page); ,当我滚动大约10页时就会发生这种情况。并点击此链接 适用于 iPhone / iPad / iO 的快速且精益的 PDF 查看器 - 提示和提示?,我尝试在发生内存警告时释放 CGPDFDocumentRef,但结果是CGPDFDocumentRef并没有释放所有的缓存,而只是释放最近的页面,所以内存仍然一路增加。我以为bug已经修复了?如何完全释放CGPDFDocumentRef???
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???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
在
drawLayer
中。由于每次调用它时都会创建一个新的文档引用。相反,在您的视图控制器中创建单个文档引用并每次都使用它。使用这一行代替
CGPDFPageRelease(页面);
Dont use
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);