12

如何截屏 tableView 的所有内容?(所有内容 = 可见区域 + 不可见区域)

我试过这个:

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

self.imageView.image = image1;

但它不起作用,我的意思是它只是屏幕截图可见区域:(


我解决了:))

这是代码:)

+ (UIImage *)captureView:(UIScrollView *)view inContentRect:(CGRect)rect{
    UIImage* image = nil;

    CGPoint savedContentOffset = view.contentOffset;
    CGRect savedFrame = view.frame;

    UIGraphicsBeginImageContextWithOptions(view.contentSize, 1, 0);
    view.contentOffset = CGPointZero;
    view.frame = CGRectMake(0, 0, view.contentSize.width, view.contentSize.height);

    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();

    view.contentOffset = savedContentOffset;
    view.frame = savedFrame;

    UIGraphicsEndImageContext();

    // after all of this, crop image to needed size
    return [Utils cropImage:image toRect:rect];                                 
}
4

3 回答 3

5
+ (UIImage *)captureView:(UIScrollView *)view inContentRect:(CGRect)rect{
    UIImage* image = nil;

    CGPoint savedContentOffset = view.contentOffset;
    CGRect savedFrame = view.frame;

    UIGraphicsBeginImageContextWithOptions(view.contentSize, 1, 0);
    view.contentOffset = CGPointZero;
    view.frame = CGRectMake(0, 0, view.contentSize.width, view.contentSize.height);

    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();

    view.contentOffset = savedContentOffset;
    view.frame = savedFrame;

    UIGraphicsEndImageContext();

    // after all of this, crop image to needed size
    return [Utils cropImage:image toRect:rect];                                 
}
于 2014-10-17T10:18:00.990 回答
2

您必须contentSize.height用于获取 tableview 的高度

尝试这个

UIGraphicsBeginImageContext(self.tableView.contentSize.height);
[self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.imageView.image = image1;
于 2013-09-03T11:14:52.580 回答
0

是的,你可以这样做。我无法提供你想要的工作代码,我只能指导你如何做到这一点。在循环中,您必须通过更改表格的所有部分来渲染表格contentOffset并将每个图像放入数组中。在获得表格视图的所有部分后,您必须将表格的各个部分连接到一个图像中(在 cgimagecontext 中绘制每个图像,记住为结果图像设置正确的大小)。就是这样,这并不难:) 希望这会有所帮助,祝你好运!

于 2013-09-03T12:54:23.067 回答