我有一个 UITableView,每个单元格中都显示一个从 pdf 创建的 UIImage。但现在的表现却非常糟糕。这是我用来从 PDF 生成 UIImage 的代码。
创建 CGPDFDocumentRef 和 UIImageView(在 cellForRowAtIndexPath 方法中):
...
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)formula.icon, NULL, NULL);
CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[self imageFromPDFWithDocumentRef:documentRef]];
...
生成 UIImage:
- (UIImage *)imageFromPDFWithDocumentRef:(CGPDFDocumentRef)documentRef {
CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1);
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));
CGContextDrawPDFPage(context, pageRef);
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
我可以做些什么来提高速度并保持较低的内存?
最佳答案
在运行时为每个单元格从 PDF 生成图像似乎对性能造成巨大影响。
您应该尝试考虑在后台线程中执行实际渲染并存储渲染结果UIImages
并在运行时将它们填充到UITableView
中。这至少会释放 UI 线程并增加应用程序的响应能力。
对于尚未渲染的单元格,您可以显示“正在加载”消息或添加“事件指示器”微调器。
关于iphone - 提高 iPhone 上 pdf 渲染的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2938865/