首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS检漏仪CGContextDrawPDFPage

iOS检漏仪CGContextDrawPDFPage
EN

Stack Overflow用户
提问于 2012-05-01 07:51:25
回答 2查看 1.6K关注 0票数 1

我知道这个问题已经问过好几次了,但我无法解决我的特殊情况。CGContextDrawPDFPage被表示为泄漏仪器中的泄漏。另外,当运行这段代码时,应用程序崩溃,我确信这是由于内存问题造成的。

代码语言:javascript
复制
    pdfURLDocument = [[NSURL alloc] initFileURLWithPath: [docsDir stringByAppendingPathComponent:documentName]];
    pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)pdfURLDocument);
    [pdfURLDocument release];

    page = CGPDFDocumentGetPage(pdfDocument, 1);
    CGPDFPageRetain(page);

    // determine the size of the PDF page
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    pdfScaleWidth = (1/((CGFloat) gridSizeDocument)) * self.frame.size.width/pageRect.size.width;
    pdfScaleHeight = (1/((CGFloat) gridSizeDocument)) * self.frame.size.height/pageRect.size.height;
    pageRect.size = CGSizeMake(pageRect.size.width*pdfScaleWidth, pageRect.size.height*pdfScaleHeight);


    // Create a low res image representation of the PDF page        
    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // First fill the background with white.
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context,pageRect);

    CGContextSaveGState(context);
    // Flip the context so that the PDF page is rendered
    // right side up.
    CGContextTranslateCTM(context, 0.0, pageRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Scale the context so that the PDF page is rendered 
    // at the correct size for the zoom level.
    CGContextScaleCTM(context, pdfScaleWidth, pdfScaleHeight);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);

    backgroundImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    CGPDFPageRelease(page);

此外,我还在dealloc方法中包括了CGPDFPageRelease(page);。此外,它可能有助于补充说,它可以很好地工作小型文档,但只崩溃的大型文档。然而,内存泄漏仍然存在于较小的内存泄漏中。

EN

回答 2

Stack Overflow用户

发布于 2013-07-17 13:27:28

我知道这是个老问题,但有两点意见:

  1. 您需要发布您的pdfDocument

但是,CGPDFDocumentRelease(pdfDocument);

  • You不应该用CGPDFPageRelease(page)发布page,因为这是一个自动释放的对象,并且您不拥有它(当然,除非您在CGPDFPageRetain中保留了它)。

如果您使用静态分析器(Xcode的“产品”菜单上的“分析”),它应该指出这两个问题。

根本的问题是CGContextDrawPDFPage在6.0之前的iOS版本中泄漏。

票数 1
EN

Stack Overflow用户

发布于 2012-05-01 10:38:17

发行版需要在页面被使用后发布,而不是之前。因此,首先,将CGPDFPageRelease(page)移动到这个代码块中,看看这是否有帮助。而且,这个问题可能与存储在CGPDFDocumentRef变量中的pdf有关。如果上面的内容没有帮助,那么如果您展示如何获得引用,以及您保留和释放它的位置,那将是很好的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10395017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档