views:

573

answers:

2

Can someone tell me what I am doing wrong here? I use this method to flip through pages in a PDF. But something in the code seems to not be released properly because every-time I pull a PDF page that contains an image my memory footprint increases. I am fairly new to CoreGraphics, and can't for the life of me figure out where this method would leak memory.

-(UIImage *)pageAtIndex:(NSInteger)pageNumber withWidth:(CGFloat)width andHeight:(CGFloat)height {
    if((pageNumber>0) && (pageNumber<=pageCount)) {
        CGFloat scaleRatio; // multiplier by which the PDF Page will be scaled

        UIGraphicsBeginImageContext(CGSizeMake(width, height)); 
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, pageNumber);
        CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFBleedBox);

        //Figure out the orientation of the PDF page and set the scaleRatio accordingly
        if(pageRect.size.width/pageRect.size.height < 1.0) {
            scaleRatio = height/pageRect.size.height;
        } 
        else {
            scaleRatio = width/pageRect.size.width;     
        }

        //Calculate the offset to center the image
        CGFloat xOffset = 0.0;
        CGFloat yOffset = height;
        if(pageRect.size.width*scaleRatio<width) {
            xOffset = (width/2)-(pageRect.size.width*scaleRatio/2);
        }
        else {
            yOffset = height-((height/2)-(pageRect.size.height*scaleRatio/2));  
        }
        CGContextTranslateCTM(context, xOffset, yOffset);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSaveGState(context);
        CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFBleedBox, CGRectMake(0, 0, pageRect.size.width, pageRect.size.height), 0, true);
        pdfTransform = CGAffineTransformScale(pdfTransform, scaleRatio, scaleRatio);

        CGContextConcatCTM(context, pdfTransform);
        CGContextDrawPDFPage(context, page);

        UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();

        CGContextRestoreGState(context);
        UIGraphicsEndPDFContext();
        UIGraphicsEndImageContext();

        return tempImage;
    }
    return nil;
}
+1  A: 

I think I resolved the problem thanks to the people on the apple core graphics mailing list. It seems that CGPDFDocument caches data between calls but never releases it. This appears to be a bug in CoreGraphics. I was told that the only real way around this is to load and unload the PDF every time I pull a page.

carloe
Did you submit it to radar?
Sneakyness
Thanks man. Do we know if it's been fixed yet?
Paul Shapiro
+1  A: 

You probably weren't releasing something. Gotta check for things like CGPDFPageRetain(<CGPDFPageRef page>) and CGPDFPageRelease(<CGPDFPageRef page>).

Sneakyness