Iphone CGContextDrawPDFPage占用大量内存
我有一个PDF文件,我想在大纲的形式绘制。我想在文档上绘制前几页,每一页都在自己的UIImage中,以便在按钮上使用,这样当单击时,主显示将导航到单击的页面 然而,CGContextDrawPDFPage在尝试绘制页面时似乎使用了大量内存。尽管图像的高度应该只有100px左右,但应用程序在绘制一个页面时会崩溃,根据仪器,该应用程序仅为一个页面分配约13MB的内存 以下是绘图代码:Iphone CGContextDrawPDFPage占用大量内存,iphone,cocoa-touch,pdf,core-graphics,Iphone,Cocoa Touch,Pdf,Core Graphics,我有一个PDF文件,我想在大纲的形式绘制。我想在文档上绘制前几页,每一页都在自己的UIImage中,以便在按钮上使用,这样当单击时,主显示将导航到单击的页面 然而,CGContextDrawPDFPage在尝试绘制页面时似乎使用了大量内存。尽管图像的高度应该只有100px左右,但应用程序在绘制一个页面时会崩溃,根据仪器,该应用程序仅为一个页面分配约13MB的内存 以下是绘图代码: //Note: This is always called in a background thread, but
//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);
}
有没有更好的方法来绘制不占用大量内存的图像?看看我在github上的PDF图像切片器代码: 设备上应该有足够的内存,13 MB的分配不会导致应用程序死机。是否每次渲染PDF时都会耗尽自动释放池?您可能还希望将渲染缓存到UIImage中,这样它就不必在每次显示时都进行渲染。尝试添加:
CGContextSetInterpolationQuality(g, kCGInterpolationHigh);
CGContextSetRenderingIntent(g, kCGRenderingIntentDefault);
之前:
CGContextDrawPDFPage(g, m_page);
我有一个类似的问题,添加上面的2函数调用导致渲染使用的内存减少了5倍。可能是CGContextXXX绘图函数中的错误好吧,这个问题不会通过更改结果来解决,因为它在尝试绘制第一幅图像时崩溃。考虑到当时绘制图像不是唯一的事情(它还使用UIWebView显示完整的PDF文件),13MB可能太多了。嗨,Ed Marty,你有没有找到解决问题的方法。我也面临同样的问题。如果你发现任何sol,请与我分享。谢谢,在这个线程完成后,您是如何更新视图的?内存大大减少,渲染时间大大加快(这比内存使用更让我痛苦)。wtf,这些应该是默认值。谢天谢地,我无意中找到了你的答案,约翰——谢谢。