开发者

CGContextDrawPDFPage Leak 100%

开发者 https://www.devze.com 2023-04-07 05:07 出处:网络
I try to Draw a pdf with CoreGraphics, everything work fine but in instrument there is a 100% leak on : CGContextDrawPDFPage(ctx, page2);

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

  开发者_Go百科      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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号