ios - 绘制用于查看 iOS 6.1 的 PDF 页面的内存问题

标签 ios objective-c ios6

我在一个应该显示(大)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/

相关文章:

ios - 从 Nib 加载 UITableViewCell 子类时给出错误的类型

ios - 如果以编程方式创建,则无法访问 UIImageView

objective-c - 用 UIImage 组合 UIActivityTypeMessage

ios6 - UISearchBar 在 iOS 6 上不接受键盘输入但接受 Siri 输入

打开新窗口时的 iOS UIWebView 事件

ios - Xcode下架构x86_64的重复符号

iphone - 如何验证 NSData 是 PDF?

objective-c - UIScrollViewDelegate scrollViewWillEndDragging :withVelocity:targetContentOffset: warning

有意向已释放对象发送消息的 Objective-c 示例代码

ios - 如何根据模式使用 swift 在 Xcode 中设置背景图像?