【问题标题】:Loading a PDF on the iPad is way too slow在 iPad 上加载 PDF 太慢了
【发布时间】:2011-07-25 11:44:11
【问题描述】:

我正在开发一个使用 CoreGraphics 呈现 PDF 文件的应用程序。我一次显示一页 PDF。在viewDidLoad 我有以下代码:

NSString *pdfFullPath = [[NSBundle mainBundle] pathForResource:@"catalogue" ofType:@"pdf"];
pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pdfFullPath]);

currentPage = [[_dataObject description] intValue];

[pdfView setImage:[self imageFromPDF:pdf withPageNumber:currentPage withScale:1.5]];

[_dataObject description] 包含当前页码作为字符串。

然后我有这个渲染 PDF 的方法:

- (UIImage *)imageFromPDF:(CGPDFDocumentRef)_pdf withPageNumber:(NSUInteger)pageNumber withScale:(CGFloat)scale
{
    if(pageNumber > 0 && pageNumber <= CGPDFDocumentGetNumberOfPages(_pdf))
    {
        CGPDFPageRef pdfPage = CGPDFDocumentGetPage(_pdf, pageNumber);
        CGRect tmpRect = CGPDFPageGetBoxRect(pdfPage,kCGPDFMediaBox);
        CGRect rect = CGRectMake(tmpRect.origin.x, tmpRect.origin.y, tmpRect.size.width * scale, tmpRect.size.height * scale);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0, rect.size.height);
        CGContextScaleCTM(context, scale, -scale);
        CGContextDrawPDFPage(context, pdfPage);
        UIImage *pdfImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return pdfImage;
    }

    return nil;
}

这似乎是一个非常缓慢的过程,它会在调用 imageFromPDF 时完全锁定应用程序,因此我正在寻找任何方法来优化该过程以使其更快。有什么想法吗?

【问题讨论】:

    标签: performance ipad pdf core-graphics render


    【解决方案1】:

    我最终将 PDF 的每一页保存为 iPad 上的图像。图像的名称是页码。当我翻到新页面时,我会检查文件是否存在于 iPad 上。例如。对于第 4 页,我会检查“4.jpg”是否存在,如果存在,我将显示“4.jpg”,否则我将呈现页面并保存。

    这种方法可能会在 iPad 上占用大量空间,但对我来说这并不是一个真正的问题,因为该应用程序最终不会出现在 App Store 上,而是会用于公司的销售人员。

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 2018-02-26
      相关资源
      最近更新 更多