【问题标题】:Increase performance on iphone at pdf rendering提高 iphone 在 pdf 渲染时的性能
【发布时间】:2011-02-25 16:50:19
【问题描述】:

我有一个 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;
} 

我可以做些什么来提高速度并保持低内存?

【问题讨论】:

  • 嗨,我跟你一样。我不知道代码中的 formula.icon 是什么。请你能提供更多吗?太赫兹

标签: iphone performance memory pdf uiimage


【解决方案1】:

在运行时为每个单元格从 PDF 生成图像似乎对性能造成了巨大影响。

您应该尝试考虑在后台线程中执行实际渲染并将渲染结果存储在UIImages 中并在运行时将它们填充到UITableView 中。这至少会释放 UI 线程并增加应用程序的响应能力。

对于尚未呈现的单元格,您可以显示“正在加载”消息或添加“活动指示器”微调器。

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 1970-01-01
    • 2012-04-17
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2017-05-17
    • 2012-06-18
    相关资源
    最近更新 更多