如何捕获完整tableView ios的屏幕截图

时间:2021-08-31 00:08:29

I have a tableView having 30 rows and I also have a View on the top of tableView(Not in the tableview header) ,I want to capture the complete screen shot of the screen including the View and all the row of tableview but i can only able to capture the visible rows of tableview and the view .Please help me and thanks in advance. Here is my Code and the screen shot of simulator. Note(I don't want my View to be in tableview header because it will also scroll when we scroll the tableview thats why the view is fixed)

我有一个有30行的tableView,我在tableView的顶部也有一个View(不在tableview标题中),我想捕获屏幕的完整屏幕截图,包括View和tableview的所有行,但我只能能够捕获可见的tableview和视图行。请帮助我,并提前感谢。这是我的代码和模拟器的屏幕截图。注意(我不希望我的View在tableview标题中,因为当我们滚动tableview时它也会滚动,这就是修复视图的原因)

- (void)viewDidLoad {
    [super viewDidLoad];


    _myTableView.delegate = self;
    _myTableView.dataSource = self;

     UIBarButtonItem *next = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextButtonAction)];
     UIBarButtonItem *screenShot = [[UIBarButtonItem alloc]initWithTitle:@"Screenshot" style:UIBarButtonItemStylePlain target:self action:@selector(screenshotButtonAction)];

    self.navigationItem.leftBarButtonItem = screenShot;
    self.navigationItem.rightBarButtonItem = next;


 }

-(void)nextButtonAction
{

    NextViewController *myVc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"NextViewController"];
     NSLog(@"value of image is %@",viewImage);
     myVc.myImage = viewImage;
    [self.navigationController pushViewController:myVc animated:YES];

}

-(void)screenshotButtonAction
{

    UIView *viewToRender = self.myTableView;
    CGPoint contentOffset = self.myTableView.contentOffset;
     UIGraphicsBeginImageContext(viewToRender.bounds.size);
     CGContextRef ctx = UIGraphicsGetCurrentContext();
      CGContextTranslateCTM(ctx, 0,-contentOffset.y ); //-contentOffset.y
     [viewToRender.layer renderInContext:ctx];
     viewImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
  }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return  30;
 }
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     static   NSString *myString = @"reused";
     UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:myString];
    if (Cell == nil)
    {
         Cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myString];
    }
     Cell.textLabel.text = [NSString stringWithFormat:@"This is Cell %ld",indexPath.row];
    return Cell;
 }

如何捕获完整tableView ios的屏幕截图

1 个解决方案

#1


1  

I've changed this code for Swift 3 from Getting a screenshot of a UIScrollView, including offscreen parts

我已经从获取UIScrollView的屏幕截图中更改了Swift 3的代码,包括屏幕外部分

func screenshot() -> UIImage{
var image = UIImage();
    UIGraphicsBeginImageContextWithOptions(self.tableView.contentSize, false, UIScreen.main.scale)

    // save initial values
    let savedContentOffset = self.tableView.contentOffset;
    let savedFrame = self.tableView.frame;
    let savedBackgroundColor = self.tableView.backgroundColor

    // reset offset to top left point
    self.tableView.contentOffset = CGPoint(x: 0, y: 0);
    // set frame to content size
    self.tableView.frame = CGRect(x: 0, y: 0, width: self.tableView.contentSize.width, height: self.tableView.contentSize.height);
    // remove background
    self.tableView.backgroundColor = UIColor.clear

    // make temp view with scroll view content size
    // a workaround for issue when image on ipad was drawn incorrectly
    let tempView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.contentSize.width, height: self.tableView.contentSize.height));

    // save superview
    let tempSuperView = self.tableView.superview
    // remove scrollView from old superview
    self.tableView.removeFromSuperview()
    // and add to tempView
    tempView.addSubview(self.tableView)

    // render view
    // drawViewHierarchyInRect not working correctly
    tempView.layer.render(in: UIGraphicsGetCurrentContext()!)
    // and get image
    image = UIGraphicsGetImageFromCurrentImageContext()!;

    // and return everything back
    tempView.subviews[0].removeFromSuperview()
    tempSuperView?.addSubview(self.tableView)

    // restore saved settings
    self.tableView.contentOffset = savedContentOffset;
    self.tableView.frame = savedFrame;
    self.tableView.backgroundColor = savedBackgroundColor

    UIGraphicsEndImageContext();

    return image
}

#1


1  

I've changed this code for Swift 3 from Getting a screenshot of a UIScrollView, including offscreen parts

我已经从获取UIScrollView的屏幕截图中更改了Swift 3的代码,包括屏幕外部分

func screenshot() -> UIImage{
var image = UIImage();
    UIGraphicsBeginImageContextWithOptions(self.tableView.contentSize, false, UIScreen.main.scale)

    // save initial values
    let savedContentOffset = self.tableView.contentOffset;
    let savedFrame = self.tableView.frame;
    let savedBackgroundColor = self.tableView.backgroundColor

    // reset offset to top left point
    self.tableView.contentOffset = CGPoint(x: 0, y: 0);
    // set frame to content size
    self.tableView.frame = CGRect(x: 0, y: 0, width: self.tableView.contentSize.width, height: self.tableView.contentSize.height);
    // remove background
    self.tableView.backgroundColor = UIColor.clear

    // make temp view with scroll view content size
    // a workaround for issue when image on ipad was drawn incorrectly
    let tempView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.contentSize.width, height: self.tableView.contentSize.height));

    // save superview
    let tempSuperView = self.tableView.superview
    // remove scrollView from old superview
    self.tableView.removeFromSuperview()
    // and add to tempView
    tempView.addSubview(self.tableView)

    // render view
    // drawViewHierarchyInRect not working correctly
    tempView.layer.render(in: UIGraphicsGetCurrentContext()!)
    // and get image
    image = UIGraphicsGetImageFromCurrentImageContext()!;

    // and return everything back
    tempView.subviews[0].removeFromSuperview()
    tempSuperView?.addSubview(self.tableView)

    // restore saved settings
    self.tableView.contentOffset = savedContentOffset;
    self.tableView.frame = savedFrame;
    self.tableView.backgroundColor = savedBackgroundColor

    UIGraphicsEndImageContext();

    return image
}