I am using the following code to render a PDF page. However, it uses a lot of memory (2-3 MB per page).
In device logs, I see:
<Error>: CGBitmapContextInfoCreate: unable to allocate 2851360 bytes for bitmap data
I really don't need the bitmap to display at 8 bits per color channel. How can I change the code to appear in grayscale or fewer bits per channel?
I would also be okay with a solution in which a bitmap is displayed with a maximum resolution of x / y, and then the resulting image is scaled to the required size. After that, the PDF will be displayed in detail using CATiledLayer.
Also, according to Apple documentation, CGBitmapContextCreate()returns NIL if the context cannot be created (due to memory). But in MonoTouch there is only a constructor for creating a context, so I can not check whether the creation was unsuccessful or not. If I were able, I could just skip the image of the applicant.
UIImage oBackgroundImage= null;
using(CGColorSpace oColorSpace = CGColorSpace.CreateDeviceRGB())
using(CGBitmapContext oContext = new CGBitmapContext(null, iWidth, iHeight, 8, iWidth * 4, oColorSpace, CGImageAlphaInfo.PremultipliedFirst))
{
oContext.SetFillColor(1f, 1f, 1f, 1f);
oContext.FillRect(oTargetRect);
RectangleF oCaptureRect = new RectangleF(0, 0, oTargetRect.Size.Width / fScaleToApply, oTargetRect.Size.Height / fScaleToApply);
CGAffineTransform oDrawingTransform = oPdfPage.GetDrawingTransform(CGPDFBox.Media, oCaptureRect, 0, true);
oContext.ScaleCTM(fScaleToApply, fScaleToApply);
oContext.ConcatCTM(oDrawingTransform);
oContext.InterpolationQuality = CGInterpolationQuality.Medium;
oContext.SetRenderingIntent (CGColorRenderingIntent.Default);
oContext.DrawPDFPage(oPdfPage);
using(CGImage oImage = oContext.ToImage())
{
oBackgroundImage = UIImage.FromImage( oImage );
}
}