我有一個PDF文件,我想以大綱形式繪製。我想在文檔中的前幾頁上繪製它們各自的UIImage,以便在按鈕上使用,以便單擊時主顯示將導航到單擊的頁面。佔用大量內存的CGContextDrawPDFPage
但是,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);
}
有沒有更好的方式來得出這樣的形象,不佔用大量的內存?
在完成此線程後,您是如何更新視圖的? – 2011-01-27 21:05:35