Reputation: 33048
I'm using the code below to render a preview of a PDF page. However it is using loads of memory (2-3MB per page).
In the device logs I see:
<Error>: CGBitmapContextInfoCreate: unable to allocate 2851360 bytes for bitmap data
I really don't need the bitmap to be rendered in 8bits per color channel. How can I change the code to have it rendered in grayscale or less bits per channel?
I would also be fine with a solution where the bitmap is rendered in a maximum resolution of x/y and then the resulting image is zoomed to the requested size. The PDF will be rendered in detail afterwards by a CATiledLayer
anyway.
Also according to Apple's documentation, CGBitmapContextCreate()
returns NIL if the context cannot be created (because of memory). But in MonoTouch there is only the constructor to create a context, hence I'm unable to check if creation failed or not.
If I was able to, I could just skip the pretender image.
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 );
}
}
Upvotes: 3
Views: 2158
Reputation: 19335
Also according to Apple's documentation, CGBitmapContextCreate() returns NIL if the context cannot be created (because of memory). But in MonoTouch there is only the constructor to create a context, hence I'm unable to check if creation failed or not. If I was able to, I could just skip the pretender image.
This is actually easy:
CGBitmapContext context;
try {
context = new CGBitmapContext (...);
} catch (Exception ex) {
context = null;
}
if (context != null) {
using (context) {
...
}
}
or you could also just surround the entire using clause in an exception handler:
try {
using (var context = new CGBitmapContext (...)) {
...
}
} catch {
// we failed
oBackgroundImage = null;
}
Upvotes: 2
Reputation: 43553
I really don't need the bitmap to be rendered in 8bits per color channel.
...
using(CGColorSpace oColorSpace = CGColorSpace.CreateDeviceRGB())
Have you tried to provide a different color space ?
where the bitmap is rendered in a maximum resolution of x/y
...
using(CGBitmapContext oContext = new CGBitmapContext(null, iWidth, iHeight, 8, iWidth * 4, oColorSpace, CGImageAlphaInfo.PremultipliedFirst))
You can control the bitmap size too and other parameters that directly affect how much memory is required by the bitmap.
Also according to Apple's documentation, CGBitmapContextCreate() returns NIL if the context cannot be created (because of memory).
If an invalid object (like null
) is returned then the C# instance will have an Handle
equals to IntPtr.Zero
. This is true for any ObjC object, since init
can return nil
and a .NET constructor cannot return null
.
Upvotes: 2