CGContextDrawPDFPage占用大量内存 [英] CGContextDrawPDFPage taking up large amounts of memory

查看:926
本文介绍了CGContextDrawPDFPage占用大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF档案,我想以大纲形式绘制。我想要绘制文档中的前几个页面,每个页面都在自己的UIImage中用于按钮,以便在单击时,主显示将导航到所单击的页面。

I have a PDF file that I want to draw in outline form. I want to draw the first several pages on the document each in their own UIImage to use on a button so that when clicked, the main display will navigate to the clicked page.

然而,CGContextDrawPDFPage似乎在尝试绘制页面时使用大量的内存。即使图像只有大约100像素高,应用程序崩溃,特别是绘制一个页面,据仪器,分配大约13 MB的内存只为一页。

However, CGContextDrawPDFPage seems to be using copious amounts of memory when attempting to draw the page. Even though the image is only supposed to be around 100px tall, the application crashes while drawing one page in particular, which according to Instruments, allocates about 13 MB of memory just for the one page.

这是代码绘制:

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

有没有更好的方法来绘制这个图像,内存量?

Is there a better way to draw this image that doesn't take up huge amounts of memory?

推荐答案

尝试添加:

CGContextSetInterpolationQuality(g, kCGInterpolationHigh);
CGContextSetRenderingIntent(g, kCGRenderingIntentDefault); 

之前:

CGContextDrawPDFPage(g, m_page);

我有一个类似的问题,添加上面的2个函数调用导致渲染使用少5倍的内存。可能是CGContextXXX绘图函数

I had a similar issue and adding the 2 function call above resulted in the rendering using 5x less memory. Might be a bug in the CGContextXXX drawing functions

这篇关于CGContextDrawPDFPage占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆