在 iPad 上加载 PDF 太慢
我正在开发一个使用 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 时完全锁定应用程序,因此我正在寻找任何方法来优化该过程,以便它可以快一点。有什么想法吗?
I am developing an application which render a PDF file using CoreGraphics. I am displaying one page of the PDF at a time. In viewDidLoad
I have the following code:
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]
contains the current page number as a string.
Then I have this method which renders the 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;
}
This seems to be a very slow process which locks the application entirely when imageFromPDF
is being called and therefore I am looking for any way to optimize the process so it would be faster. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终做的是将 PDF 的每一页保存为 iPad 上的图像。图像的名称是页码。当我翻到新页面时,我会检查 iPad 上是否存在该文件。例如,对于第 4 页,我将检查“4.jpg”是否存在,如果存在,我将显示“4.jpg”,否则我将渲染页面并保存它。
这种方法可能会占用 iPad 上的大量空间,但对于我的使用而言,这并不是真正的问题,因为该应用程序最终不会出现在 App Store 上,而是供公司的销售人员使用。
What I ended up doing is to save each page of the PDF as an image on the iPad. The name of the images is the page number. When I flip to a new page I check if the file exists on the iPad. E.g. for page 4 I would check if "4.jpg" exists, if it does, I will show "4.jpg" else I will render the page and save it.
This approach might use a lot of space on the iPad but for my use that's not really an issue as the application will not end up on App Store but will be used for salesmen in a company.