CGContextDrawPDFPage 泄漏 100%

发布于 2024-12-06 13:31:23 字数 3034 浏览 0 评论 0原文

我尝试使用 CoreGraphics 绘制 pdf,一切正常,但在仪器中存在 100% 泄漏:CGContextDrawPDFPage(ctx, page2); 我用 CGPDFDocumentRelease() 发布;每次我使用 CGPDFDocumentCreateWithURL(); 有什么解决方案可以发布:CGContextDrawPDFPage?

  - (void)drawRect:(CGRect)rect
        {
        if (state == 0) {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        [[UIColor whiteColor] set];
        CGContextFillRect(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height));
        CGContextGetCTM(ctx);
        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 45, -rect.size.height);
        CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(((CFURLRef)url));
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);
        CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
        CGContextScaleCTM(ctx, (678) / mediaRect.size.width,
        (rect.size.height )  / (mediaRect.size.height));
        CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
        CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
        CGContextDrawPDFPage(ctx, page);
        CGPDFDocumentRelease(pdf);
        CGContextRestoreGState(ctx);
        }
        else
        {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        [[UIColor whiteColor] set];
        CGContextFillRect(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height));

        CGContextGetCTM(ctx);
        CGContextScaleCTM(ctx, 1, -1);

        CGContextTranslateCTM(ctx, 6, -rect.size.height);

        CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(((CFURLRef)url));
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);


        CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
        CGContextScaleCTM(ctx, (497) / mediaRect.size.width,
        (rect.size.height )  / (mediaRect.size.height));

        // draw it
        CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
        CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
        CGContextDrawPDFPage(ctx, page);
        CGPDFDocumentRelease(pdf);
        CGContextRestoreGState(ctx);

        //

        CGContextSaveGState(ctx);

        CGContextGetCTM(ctx);
        CGContextScaleCTM(ctx, 1, -1);

        CGContextTranslateCTM(ctx, 506, -rect.size.height);

        CGPDFDocumentRef pdf2 = CGPDFDocumentCreateWithURL(((CFURLRef)url));
        CGPDFPageRef page2 = CGPDFDocumentGetPage(pdf2, (currentPage + 1));



        CGContextScaleCTM(ctx, (497) / mediaRect.size.width,
        (rect.size.height )  / (mediaRect.size.height));

        CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
        CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
        //Leak 100%
        CGContextDrawPDFPage(ctx, page2);
        CGPDFDocumentRelease(pdf2);
        CGContextRestoreGState(ctx);
        }
    }

我不知道为什么。有什么想法吗? 这是我的应用程序的唯一泄漏:( 我不明白泄漏来自哪里:s

PS:state = 0 = 纵向 state = 1 = 横向,所以我横向绘制 2 页。

I try to Draw a pdf with CoreGraphics, everything work fine but in instrument there is a 100% leak on : CGContextDrawPDFPage(ctx, page2);
I release with CGPDFDocumentRelease(); everytime i use CGPDFDocumentCreateWithURL();
There is any solution to release : CGContextDrawPDFPage ?

  - (void)drawRect:(CGRect)rect
        {
        if (state == 0) {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        [[UIColor whiteColor] set];
        CGContextFillRect(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height));
        CGContextGetCTM(ctx);
        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 45, -rect.size.height);
        CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(((CFURLRef)url));
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);
        CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
        CGContextScaleCTM(ctx, (678) / mediaRect.size.width,
        (rect.size.height )  / (mediaRect.size.height));
        CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
        CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
        CGContextDrawPDFPage(ctx, page);
        CGPDFDocumentRelease(pdf);
        CGContextRestoreGState(ctx);
        }
        else
        {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        [[UIColor whiteColor] set];
        CGContextFillRect(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height));

        CGContextGetCTM(ctx);
        CGContextScaleCTM(ctx, 1, -1);

        CGContextTranslateCTM(ctx, 6, -rect.size.height);

        CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(((CFURLRef)url));
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);


        CGRect mediaRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
        CGContextScaleCTM(ctx, (497) / mediaRect.size.width,
        (rect.size.height )  / (mediaRect.size.height));

        // draw it
        CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
        CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
        CGContextDrawPDFPage(ctx, page);
        CGPDFDocumentRelease(pdf);
        CGContextRestoreGState(ctx);

        //

        CGContextSaveGState(ctx);

        CGContextGetCTM(ctx);
        CGContextScaleCTM(ctx, 1, -1);

        CGContextTranslateCTM(ctx, 506, -rect.size.height);

        CGPDFDocumentRef pdf2 = CGPDFDocumentCreateWithURL(((CFURLRef)url));
        CGPDFPageRef page2 = CGPDFDocumentGetPage(pdf2, (currentPage + 1));



        CGContextScaleCTM(ctx, (497) / mediaRect.size.width,
        (rect.size.height )  / (mediaRect.size.height));

        CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
        CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
        //Leak 100%
        CGContextDrawPDFPage(ctx, page2);
        CGPDFDocumentRelease(pdf2);
        CGContextRestoreGState(ctx);
        }
    }

And I don't know why. Any idea?
This is the only leak of my app :(
I don't see where the leak come from :s

PS : state = 0 = portrait orientation
state = 1 = landscape so I draw 2 pages in landscape orientation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2024-12-13 13:31:23

看来您没有释放从 CGPDFDocumentGetPage 获取的 CGPDFPageRef 实例。

CGPDFPageRelease(page);

在适当的位置添加 page 和 page2 的内容。

Looks like you're not releasing the CGPDFPageRef instances you're getting from CGPDFDocumentGetPage.

CGPDFPageRelease(page);

Add that for page and page2 at the appropriate locations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文