gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:39:57 25 4
gpt4 key购买 nike

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

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

This is the view I want to take screenshot with full tableview and above image

最佳答案

我已将 Swift 3 的代码从 Getting a screenshot of a UIScrollView, including offscreen parts 更改为

编辑:2022 - TableView 在屏幕截图后恢复

 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 = self.tableView.backgroundColor;

// 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

// Save constraints
guard let superView = self.tableView.superview else {
return UIImage();
}

//get old constraints from table
var oldConstraints: [NSLayoutConstraint] = []
for constraint in superView.constraints {
if constraint.firstItem as? NSObject == self.tableView || constraint.secondItem as? NSObject == self.tableView{
oldConstraints.append(constraint)
}
}

// 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)

//activate table's old constraints
NSLayoutConstraint.activate(oldConstraints)

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

UIGraphicsEndImageContext();


return image
}

关于ios - 如何捕获完整的 tableView ios 屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40022566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com