2010-05-30 47 views
3

我有一个UITableView,并且在每个单元格中都显示了从pdf创建的UIImage。但现在表现非常糟糕。这是我用来从PDF生成UIImage的代码。在pdf渲染上提高iphone上的性能

创建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; 
} 

我能做些什么,以营养不良的人口增加速度,并保持内存不足?

+0

嗨,我喜欢你的。我不知道代码中的formula.icon是什么。请你能提供更多?太赫兹 – 2014-05-25 07:14:19

回答

3

在运行时为每个单元格生成来自PDF的图像似乎是一个巨大的性能影响。

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

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