Note that there are some explanatory texts on larger screens.

plurals
  1. POMonotouch: Generating UIImage from PDF leaking entire document size
    primarykey
    data
    text
    <p>In a Monotouch application the following code converts a page from a PDF document to a UIImage. It works, but it doesn't correctly dispose the PDF document. </p> <pre><code>public class KillerLeak { public static void RunOnce() { using (CGPDFDocument doc = CGPDFDocument.FromFile("DMSWorkbook.pdf")) { Console.WriteLine("Opened: pages={0}", doc.Pages); UIImage img = GetThumbImage(doc, 1000, 1); img.Dispose(); } GC.Collect(GC.MaxGeneration); GC.WaitForPendingFinalizers(); } private static UIImage GetThumbImage(CGPDFDocument document, float thumbContentSize, int pageNumber) { if ((pageNumber &lt;= 0) || (pageNumber &gt; document.Pages)) { return null; } // Calc page view size SizeF pageSize = new SizeF(768, 1024); if (pageSize.Width % 2 &gt; 0) { pageSize.Width--; } if (pageSize.Height % 2 &gt; 0) { pageSize.Height--; } // Calc target size var targetSize = new Size((int)pageSize.Width, (int)pageSize.Height); // Draw page on CGImage CGImage pageImage; using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB()) { using (CGBitmapContext context = new CGBitmapContext(null, targetSize.Width, targetSize.Height, 8, 0, rgb, CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.NoneSkipFirst)) { using (CGPDFPage pdfPage = document.GetPage(pageNumber)) { RectangleF thumbRect = new RectangleF(0.0f, 0.0f, targetSize.Width, targetSize.Height); context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f); context.FillRect(thumbRect); context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, thumbRect, 0, true)); context.SetRenderingIntent(CGColorRenderingIntent.Default); context.InterpolationQuality = CGInterpolationQuality.Default; context.DrawPDFPage(pdfPage); pageImage = context.ToImage(); } } } UIImage result = UIImage.FromImage(pageImage); pageImage.Dispose(); return result; } } </code></pre> <p>If I comment out the DrawPDFPage(...) line, the code doesn't leak. Is this a Monotouch bug or am I doing something wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload