将UITableView转换为UIImageView [英] Turning a UITableView into an UIImageView
问题描述
我有一个 UITableView
,我想抓住它的可见部分,然后将它放入 UIImage
。这样做的任何方式?
I have got a UITableView
, and I want to capture the visible portion of it, and put it into a UIImage
. Any way of doing this?
编辑
以下自答案
推荐答案
感谢您的回复。所有答案都适用于UIView,但不适用于UITableView - 至少在表格滚动时没有。滚动时,捕获的图像将显示为黑色或透明。我尝试过类似以下的解决方案。我猜人们认为答案是显而易见的,这就是为什么我被贬低 - 因为顽皮地问这么明显的问题。
Thanks for the replies. All of the answers would work with a UIView, but not with a UITableView - at least not when the table has scrolled. When scrolled, the captured image would appear black or transparent. I had tried similar solutions as the ones below before. I guess people assumed the answer was obvious, and that's why I was down-voted - for being naughty enough to ask such an obvious question.
无论如何,真正的解决方案是您必须使用表格的contentOffset:
Anyway, the real solution is that you have to use the table's 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;
}
这篇关于将UITableView转换为UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!