将自定义的UITableViewCells导出到UIImage中

我有一个UITableView与一组UITableViewCell它在其中 – 每个单元格包含一个UIView显示上下文中的一些graphics。 将这些单元格导出到一个UIImage的最佳方法是什么?

谢谢

编辑1:我知道如何从表格的可视区域创build一个图像,但是这个表格会滚动到屏幕之外,我想创build一个所有单元格的UIImage ,不仅仅是你看到的。

你应该能够通过告诉视图的图层来绘制一个自定义的graphics上下文,然后从该上下文创build一个位图CGImage。 一旦你有一个CGImage,你可以从它创build一个UIImage。 它看起来大概是这样的:

 // Create a bitmap context. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapContextForCell = CGBitmapContextCreate(nil, cell.bounds.size.width, cell.bounds.size.height, 8, 0, colorSpace, kCGImageAlphaNone); CGColorSpaceRelease(colorSpace); // Draw the cell's layer into the context. [cell.layer renderInContext:bitmapContextForCell]; // Create a CGImage from the context. CGImageRef cgImageForCell = CGBitmapContextCreateImage(bitmapContextForCell); // Create a UIImage from the CGImage. UIImage * cellImage = [UIImage imageWithCGImage:cgImageForCell]; // Clean up. CGImageRelease(cgImageForCell); CGContextRelease(bitmapContextForCell); 

这就是如何为每个单元格创build一个图像。 如果要为所有单元格创build一个图像,请使用表格视图而不是单元格。