CGContextDrawPDFPage泄漏100%

时间:2011-09-23 14:35:45

标签: pdf core-graphics memory-leaks

我尝试使用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:状态= 0 =纵向方向 state = 1 = landscape,所以我以横向绘制了2页。

1 个答案:

答案 0 :(得分:0)

看起来你没有发布你从CGPDFDocumentGetPage获得的CGPDFPageRef实例。

CGPDFPageRelease(page);

在适当的位置为页面和第2页添加。