【问题标题】:iPhone: CoreGraphics and memory managementiPhone:CoreGraphics 和内存管理
【发布时间】:2011-02-05 08:13:09
【问题描述】:

谁能告诉我我在这里做错了什么?我使用这种方法来翻阅 PDF 中的页面。但是代码中的某些内容似乎没有正确发布,因为每次我拉出包含图像的 PDF 页面时,我的内存占用都会增加。我对 CoreGraphics 还很陌生,我一生都无法弄清楚这种方法会在哪里泄漏内存。

-(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;
}

【问题讨论】:

    标签: iphone cgcontext core-graphics cgpdfdocument


    【解决方案1】:

    感谢苹果核心图形邮件列表中的人,我想我解决了这个问题。似乎 CGPDFDocument 在调用之间缓存数据但从不释放它。这似乎是 CoreGraphics 中的一个错误。有人告诉我,解决这个问题的唯一真正方法是每次拉页面时加载和卸载 PDF。

    【讨论】:

      【解决方案2】:

      你可能没有发布任何东西。必须检查CGPDFPageRetain(&lt;CGPDFPageRef page&gt;)CGPDFPageRelease(&lt;CGPDFPageRef page&gt;) 之类的内容。

      【讨论】:

        猜你喜欢
        • 2016-10-12
        • 2021-01-23
        • 2013-01-17
        • 2011-02-18
        • 2011-07-01
        • 2019-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多