首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGContextDrawPDFPage占用大量内存

CGContextDrawPDFPage占用大量内存
EN

Stack Overflow用户
提问于 2010-06-04 22:52:27
回答 2查看 7K关注 0票数 9

我有一个PDF文件,我想以轮廓的形式绘制。我希望在文档中绘制前几个页面,每个页面都在各自的UIImage中使用,以便在单击按钮时,主显示将导航到所单击的页面。

然而,在尝试绘制页面时,CGContextDrawPDFPage似乎使用了大量内存。尽管图像应该只有100px左右高,但应用程序在绘制一个页面时崩溃,根据Instruments的说法,它仅为一个页面分配了大约13MB的内存。

以下是绘制的代码:

代码语言:javascript
复制
//Note: This is always called in a background thread, but the autorelease pool is setup elsewhere
+ (void) drawPage:(CGPDFPageRef)m_page inRect:(CGRect)rect inContext:(CGContextRef) g { 
    CGPDFBox box = kCGPDFMediaBox;
    CGAffineTransform t = CGPDFPageGetDrawingTransform(m_page, box, rect, 0,YES);
    CGRect pageRect = CGPDFPageGetBoxRect(m_page, box);

    //Start the drawing
    CGContextSaveGState(g);

    //Clip to our bounding box
    CGContextClipToRect(g, pageRect);   

    //Now we have to flip the origin to top-left instead of bottom left
    //First: flip y-axix
    CGContextScaleCTM(g, 1, -1);
    //Second: move origin
    CGContextTranslateCTM(g, 0, -rect.size.height);

    //Now apply the transform to draw the page within the rect
    CGContextConcatCTM(g, t);

    //Finally, draw the page
    //The important bit.  Commenting out the following line "fixes" the crashing issue.
    CGContextDrawPDFPage(g, m_page);

    CGContextRestoreGState(g);
}

有没有更好的方法来绘制这个图像,而不占用大量的内存?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-19 22:35:57

尝试添加:

代码语言:javascript
复制
CGContextSetInterpolationQuality(g, kCGInterpolationHigh);
CGContextSetRenderingIntent(g, kCGRenderingIntentDefault); 

之前:

代码语言:javascript
复制
CGContextDrawPDFPage(g, m_page);

我遇到了类似的问题,添加上面的2个函数调用导致渲染使用的内存减少了5倍。可能是CGContextXXX绘图函数中的错误

票数 16
EN

Stack Overflow用户

发布于 2010-06-05 14:04:37

看看我在github上为PDF图像切片器编写的代码:

http://github.com/luciuskwok/Maps-Slicer

设备上应该有足够的内存,13MB的分配不会杀死应用程序。您是否在每次渲染PDF时都会耗尽自动释放池?您可能还希望将渲染缓存到UIImage中,这样就不必在每次显示时都渲染它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2975240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档