将UITableView转换为UIImageView

时间:2012-10-18 12:39:44

标签: iphone objective-c ios uitableview uiimage

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

修改

自我回答

4 个答案:

答案 0 :(得分:5)

感谢您的回复。所有答案都适用于UIView,但不适用于UITableView - 至少在表格滚动时没有。滚动时,捕获的图像将显示为黑色或透明。我尝试过类似以下的解决方案。我猜人们认为答案是显而易见的,这就是为什么我被贬低的原因 - 因为顽皮地问这么明显的问题。

无论如何,真正的解决方案是你必须使用表格的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;
}

答案 1 :(得分:3)

UIGraphicsBeginImageContext(someView.bounds.size);
[someView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

取自this question

答案 2 :(得分:1)

- (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;

}

答案 3 :(得分:1)

UIGraphicsBeginImageContext(tableView.bounds.size);
[tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

也许这些帖子会有所帮助 Rendering UIView with its children

Saving UIView contents in iOS 4 with real size of the images inside (i.e. scale contentes up for save)

Safe way to render UIVIew to an image on background thread?

相关问题