gpt4 book ai didi

c# - 单点触控 : Memory leak when drawing PDF on a custom graphics context

转载 作者:行者123 更新时间:2023-11-30 21:10:35 28 4
gpt4 key购买 nike

我有一个基本上是花哨的 PDF 阅读器的应用程序。我从 Internet 下载 PDF 并为该 PDF 生成缩略图。然而,当我生成这些缩略图时,似乎正在分配 很多 内存(使用 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)。

最佳答案

不确定您的代码中的问题出在哪里,但这是我生成 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;
}

关于c# - 单点触控 : Memory leak when drawing PDF on a custom graphics context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8396698/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com