MonoTouch:在自定义图形上下文中绘制PDF时内存泄漏

时间:2011-12-06 07:43:52

标签: c# pdf xamarin.ios

我的应用程序基本上是一个花哨的PDF阅读器。我从互联网上下载PDF并为该PDF生成缩略图。但是,似乎当我生成这些缩略图时,正在分配 lot 内存(使用Instruments检查),有时这部分内容由GC收集,但最后,我的应用程序放弃了。在为单个PDF(100x100拇指,约60页)生成缩略图时,我的内存使用量高达38Mb。

我一次生成一个缩略图,存储它然后重复该过程,因此在任何情况下,内存中应该只有一个缩略图(至少在生成它们时)。我生成缩略图的代码如下所示:

public UIImage GetPageThumbnail(int pageNumber, SizeF size)
{
    //If using retina display, make sure to scale-up the thumbnail as well.
    size.Width = size.Width * UIScreen.MainScreen.Scale;
    size.Height = size.Height * UIScreen.MainScreen.Scale;

    UIGraphics.BeginImageContext(size);
    CGContext tempContext = UIGraphics.GetCurrentContext();
    CGPDFPage page = Document.GetPage(pageNumber);

    RectangleF drawArea = new RectangleF(new PointF(0f, 0f), size);
    CGAffineTransform transform = page.GetDrawingTransform( CGPDFBox.Crop, drawArea, 180, true); //fit PDF to context
    transform.xx = -transform.xx;   // }
    transform.x0 = 0;               // }flip horizontally

    //Console.WriteLine("XX: " + transform.xx + ", YX:" + transform.yx + ", XY:" + transform.xy + ", YY:" + transform.yy + ", X0:" + transform.x0 + ", Y0:" + transform.y0);

    tempContext.ConcatCTM(transform);

    tempContext.DrawPDFPage (page);
    UIImage returnImage = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();
    return returnImage;
}

我已经尝试过明确地处理上下文和PDF页面,但这没有任何效果(实际上它看起来更糟,但是只需要用一点盐就可以了。)

我在MonoTouch和PDF上看到了一些关于内存泄漏的帖子(基本上是this post),但这已经很老了。我正在使用最新的MonoTouch(5.0.2)。

1 个答案:

答案 0 :(得分:4)

不确定代码中的问题在哪里,但这是我生成PDF页面大拇指的代码。它运作完美。也许它会帮助你。我认为你的问题可能就是你在做的时候对你所做的事情。

public static UIImageView GetLowResPagePreview (CGPDFPage oPdfPage, RectangleF oTargetRect)
        {
            RectangleF oOriginalPdfPageRect = oPdfPage.GetBoxRect (CGPDFBox.Media);
            RectangleF oPdfPageRect = PdfViewerHelpers.RotateRectangle( oPdfPage.GetBoxRect (CGPDFBox.Media), oPdfPage.RotationAngle);

            // Create a low res image representation of the PDF page to display before the TiledPDFView
            // renders its content.
            int iWidth = Convert.ToInt32 ( oPdfPageRect.Size.Width );
            int iHeight = Convert.ToInt32 ( oPdfPageRect.Size.Height );
            CGColorSpace oColorSpace = CGColorSpace.CreateDeviceRGB();
            CGBitmapContext oContext = new CGBitmapContext(null, iWidth, iHeight, 8, iWidth * 4, oColorSpace, CGImageAlphaInfo.PremultipliedLast);

            // First fill the background with white.
            oContext.SetFillColor (1.0f, 1.0f, 1.0f, 1.0f);
            oContext.FillRect (oOriginalPdfPageRect);
            // Scale the context so that the PDF page is rendered 
            // at the correct size for the zoom level.
            oContext.ConcatCTM ( oPdfPage.GetDrawingTransform ( CGPDFBox.Media, oPdfPageRect, 0, true ) );
            oContext.DrawPDFPage (oPdfPage);
            CGImage oImage = oContext.ToImage();
            UIImage oBackgroundImage = UIImage.FromImage( oImage );
            oContext.Dispose();
            oImage.Dispose ();
            oColorSpace.Dispose ();

            UIImageView oBackgroundImageView = new UIImageView (oBackgroundImage);
            oBackgroundImageView.Frame = new RectangleF (new PointF (0, 0), oPdfPageRect.Size);
            oBackgroundImageView.ContentMode = UIViewContentMode.ScaleToFill;
            oBackgroundImageView.UserInteractionEnabled = false;
            oBackgroundImageView.AutoresizingMask = UIViewAutoresizing.None;
            return oBackgroundImageView;
        }