我在一个应该显示(大)PDF 的简单 iPad/iPhone 应用程序上遇到了一些内存问题。这就是我所做的。
我有一个带有 5 个 subview 的 ScrollView 。每个 subview 显示我的 PDF 的一页。始终显示一页,每侧预加载 2 页。如果滚动,将获取不再需要的 View ,呈现要预加载的新 PDF 页面,并且 View 将在 ScrollView 内重新定位。
除了内存问题之外,一切都工作得很好。如果我分析内存泄漏,事件字节会从 4MB 增长到 40MB 以上。总字节增长至 600 MB 以上。 我认为实时字节数不应该真正增长,因为我随时都有相同的 5 个 View 。或者我读取的值不正确?
使用ARC。
这是呈现 PDF 页面的代码。
-(void)drawRect:(CGRect)inRect{
self.ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(self.ctx);
CGRect cropBox = CGPDFPageGetBoxRect(self.pdfPage, kCGPDFCropBox);
CGRect targetRect = [self bounds];
CGFloat xScale = targetRect.size.width / cropBox.size.width;
CGFloat yScale = targetRect.size.height / cropBox.size.height;
CGFloat scaleToApply = xScale < yScale ? xScale : yScale;
CGFloat newWidth = cropBox.size.width * scaleToApply;
CGFloat newHeight = cropBox.size.height * scaleToApply;
CGFloat xOffset = (targetRect.size.width - newWidth) / 2;
CGFloat yOffset = (targetRect.size.height - newHeight) / 2;
CGContextTranslateCTM(self.ctx, xOffset, [self bounds].size.height - yOffset);
CGContextScaleCTM(self.ctx, 1.0, -1.0);
CGContextConcatCTM(self.ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
CGContextSetInterpolationQuality(self.ctx, kCGInterpolationHigh);
CGContextSetRenderingIntent(self.ctx, kCGRenderingIntentDefault);
CGContextDrawPDFPage(self.ctx, self.pdfPage);
CGContextRestoreGState(self.ctx);
}
如果不再需要旧的 PDF 页面,这是使 View 重新呈现到新的 PDF 页面的方法。
- (void)redrawPdfPage:(CGPDFPageRef)pPdfPage withPageNum:(int)pPageNum {
self.pdfPage = pPdfPage;
self.pageNum = pPageNum;
CGRect r = [self frame];
r.origin.x = r.size.width * (self.pageNum - 1);
[self setFrame:r];
[self setNeedsDisplay];
}
对此的任何建议将不胜感激,因为我正在尝试找出如何释放旧 PDF 页面的内存数天......
最佳答案
使用 ARC,您仍然需要花一些精力来管理 Foundation 对象。您可以使用CGPDFPageRelease
释放旧页面。或者,您可以将适当的属性添加到 pdfPage
属性声明中,如 this answer 中所述。 .
关于ios - 绘制用于查看 iOS 6.1 的 PDF 页面的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17209558/