CGContextDrawPDFPage占用大量内存

Ed *_*rty 9 pdf iphone cocoa-touch core-graphics

我有一个PDF文件,我想以大纲形式绘制.我想在他们自己的UIImage中绘制文档中的前几页,以便在按钮上使用,这样当单击时,主显示将导航到单击的页面.

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

这是绘图的代码:

//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);
}
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法来绘制不占用大量内存的图像?

小智 16

尝试添加:

CGContextSetInterpolationQuality(g, kCGInterpolationHigh);
CGContextSetRenderingIntent(g, kCGRenderingIntentDefault); 
Run Code Online (Sandbox Code Playgroud)

之前:

CGContextDrawPDFPage(g, m_page);
Run Code Online (Sandbox Code Playgroud)

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