【发布时间】: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