我正在使用下面的代码来呈现PDF页面的预览。但是它使用大量的内存(每页2-3MB)。如何在将PDF页面呈现到CGBitmapContext时减少内存使用量?
在设备日志,我看到:
<Error>: CGBitmapContextInfoCreate: unable to allocate 2851360 bytes for bitmap data
我真的不需要位图中每个颜色通道8位来呈现。如何更改代码以使其呈现灰色或每通道较少的位数?
我也会很好的解决方案,位图以最大分辨率x/y呈现,然后生成的图像放大到所需的大小。无论如何,PDF将在后面以CATiledLayer
进行详细描述。
同样根据Apple的文档,如果无法创建上下文(由于内存),CGBitmapContextCreate()
返回NIL。但是在MonoTouch中只有构造函数来创建上下文,因此我无法检查创建是否失败。 如果我能够,我可以跳过伪装者的形象。
UIImage oBackgroundImage= null;
using(CGColorSpace oColorSpace = CGColorSpace.CreateDeviceRGB())
// This is the line that is causing the issue.
using(CGBitmapContext oContext = new CGBitmapContext(null, iWidth, iHeight, 8, iWidth * 4, oColorSpace, CGImageAlphaInfo.PremultipliedFirst))
{
// Fill background white.
oContext.SetFillColor(1f, 1f, 1f, 1f);
oContext.FillRect(oTargetRect);
// Calculate the rectangle to fit the page into.
RectangleF oCaptureRect = new RectangleF(0, 0, oTargetRect.Size.Width/fScaleToApply, oTargetRect.Size.Height/fScaleToApply);
// GetDrawingTransform() doesn't scale up, that's why why let it calculate the transformation for a smaller area
// if the current page is smaller than the area we have available (fScaleToApply > 1). Afterwards we scale up again.
CGAffineTransform oDrawingTransform = oPdfPage.GetDrawingTransform(CGPDFBox.Media, oCaptureRect, 0, true);
// Now scale context up to final size.
oContext.ScaleCTM(fScaleToApply, fScaleToApply);
// Concat the PDF transformation.
oContext.ConcatCTM(oDrawingTransform);
// Draw the page.
oContext.InterpolationQuality = CGInterpolationQuality.Medium;
oContext.SetRenderingIntent (CGColorRenderingIntent.Default);
oContext.DrawPDFPage(oPdfPage);
// Capture an image.
using(CGImage oImage = oContext.ToImage())
{
oBackgroundImage = UIImage.FromImage(oImage);
}
}
我做了一个有趣的发现:这个问题是在iPad上3只!在高分辨率的iPad 3 b/c上,位图使用2.8MB的RAM。在iPad 1和2上,相同的位图使用700k。我认为iOS中存在一个问题 - 它应该真的警告我,但它会在没有任何警告的情况下死亡。我现在将最大位图分辨率限制为700k。 – Krumelur