2010-05-30 62 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線程併爲應用程序添加響應。

對於尚未呈現的單元格,您可以顯示「加載」消息或添加「活動指示器」微調器。