代码之家  ›  专栏  ›  技术社区  ›  Ed Marty

CGContextDrawPDFPage占用大量内存

  •  8
  • Ed Marty  · 技术社区  · 16 年前

    然而,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);
    }
    

    有没有更好的方法来绘制这个不占用大量内存的图像?

    2 回复  |  直到 16 年前
        1
  •  16
  •   beryllium    10 年前

    尝试添加:

    CGContextSetInterpolationQuality(g, kCGInterpolationHigh);
    CGContextSetRenderingIntent(g, kCGRenderingIntentDefault); 
    

    CGContextDrawPDFPage(g, m_page);
    

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

        2
  •  0
  •   lucius    16 年前

    请看我在github上的PDF图像切片器代码:

    http://github.com/luciuskwok/Maps-Slicer