iPhone:CoreGraphics 和内存管理

发布于 2024-08-27 15:38:06 字数 2055 浏览 8 评论 0原文

有人可以告诉我我在这里做错了什么吗?我使用这种方法来翻阅 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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-09-03 15:38:06

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

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.

掩耳倾听 2024-09-03 15:38:06

您可能没有发布任何东西。必须检查诸如 CGPDFPageRetain()CGPDFPageRelease() 之类的内容。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文