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