iPhone: CoreGraphics and memory management

Posted by carloe on Stack Overflow See other posts from Stack Overflow or by carloe
Published on 2010-04-07T16:01:05Z Indexed on 2010/04/07 16:03 UTC
Read the original article Hit count: 686

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() retain];

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

        return tempImage;
    }
    return nil;
}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about coregraphics