【发布时间】:2011-02-27 20:22:12
【问题描述】:
我有一个 PDF 文件,我想以大纲形式绘制。我想在文档的前几页中分别绘制它们自己的 UIImage 以在按钮上使用,以便在单击时,主显示将导航到单击的页面。
但是,CGContextDrawPDFPage 在尝试绘制页面时似乎正在使用大量内存。尽管图像应该只有大约 100 像素高,但应用程序在特别是绘制一页时会崩溃,根据 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);
}
有没有更好的方法来绘制这个不占用大量内存的图像?
【问题讨论】:
-
本帖结束后你是如何更新视图的?
标签: iphone cocoa-touch pdf core-graphics