将UITableView转换成UIImageView

我有一个UITableView ,我想捕获它的可见部分,并将其放入一个UIImage 。 任何方式这样做?

编辑

自我回答如下

感谢您的答复。 所有的答案都可以使用UIView,但不能使用UITableView – 至less在表格滚动的时候不行。 滚动时,拍摄的图像将显示为黑色或透明。 我曾尝试过类似的解决scheme。 我想人们认为答案是显而易见的,这就是为什么我被低估了 – 因为调皮到问这样一个明显的问题。

无论如何,真正的解决scheme是你必须使用表的contentOffset:

 - (UIImage *) imageWithTableView:(UITableView *)tableView { UIView *renderedView = tableView; CGPoint tableContentOffset = tableView.contentOffset; UIGraphicsBeginImageContextWithOptions(renderedView.bounds.size, renderedView.opaque, 0.0); CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(contextRef, 0, -tableContentOffset.y); [tableView.layer renderInContext:contextRef]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 
 UIGraphicsBeginImageContext(someView.bounds.size); [someView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

从这个问题采取。

 - (UIImage *)captureView { //hide controls if needed CGRect rect = [yourTableView bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [yourTableview.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } 
 UIGraphicsBeginImageContext(tableView.bounds.size); [tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

也许这些post将有助于渲染UIView及其子项

在iOS 4中保存UIView的内容与实际大小的图像里面(即缩放contentes为保存)

安全的方式来渲染UIVIew后台线程上的图像?