我有一个PDF文件,我想以轮廓的形式绘制。我希望在文档中绘制前几个页面,每个页面都在各自的UIImage中使用,以便在单击按钮时,主显示将导航到所单击的页面。
然而,在尝试绘制页面时,CGContextDrawPDFPage似乎使用了大量内存。尽管图像应该只有100px左右高,但应用程序在绘制一个页面时崩溃,根据Instruments的说法,它仅为一个页面分配了大约13MB的内存。
以下是绘制的代码:
//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);
}有没有更好的方法来绘制这个图像,而不占用大量的内存?
发布于 2010-11-19 22:35:57
尝试添加:
CGContextSetInterpolationQuality(g, kCGInterpolationHigh);
CGContextSetRenderingIntent(g, kCGRenderingIntentDefault); 之前:
CGContextDrawPDFPage(g, m_page);我遇到了类似的问题,添加上面的2个函数调用导致渲染使用的内存减少了5倍。可能是CGContextXXX绘图函数中的错误
发布于 2010-06-05 14:04:37
看看我在github上为PDF图像切片器编写的代码:
http://github.com/luciuskwok/Maps-Slicer
设备上应该有足够的内存,13MB的分配不会杀死应用程序。您是否在每次渲染PDF时都会耗尽自动释放池?您可能还希望将渲染缓存到UIImage中,这样就不必在每次显示时都渲染它。
https://stackoverflow.com/questions/2975240
复制相似问题