tableview 截圖
//將tableview 生成圖片
+ (UIImage*)getTableViewimage:(UITableView*)tableView
{
? ? if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
? ? ? ? UIGraphicsBeginImageContextWithOptions(tableView.contentSize, NO, [UIScreen mainScreen].scale);
? ? }else{
? ? ? ? UIGraphicsBeginImageContext(tableView.contentSize);
? ? }
? ? //先保存原來frame 和 偏移量
? ? CGPointsavedContentOffset = tableView.contentOffset;
? ? CGRectsavedFrame =tableView.frame;
? ? CGSizecontentSize = tableView.contentSize;
? ? CGRectoldBounds = tableView.bounds;
? ? if(@available(iOS13, *)){
? ? ? ? //iOS 13 系統(tǒng)截屏需要改變tableview 的bounds
? ? ? ? [tableView.layersetBounds:CGRectMake(oldBounds.origin.x, oldBounds.origin.y, contentSize.width, contentSize.height)];
? ? }
? ? //偏移量歸零
? ? tableView.contentOffset = CGPointZero;
? ? //frame變?yōu)閏ontentSize
? ? tableView.frame = CGRectMake(0, 0,tableView.contentSize.width, tableView.contentSize.height);
? ? //截圖
? ? [tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
? ? if(@available(iOS13,*)){
? ? ? ? [tableView.layersetBounds:oldBounds];
? ? }
? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
? ? UIGraphicsEndImageContext();
? ? //還原frame 和 偏移量
? ? tableView.contentOffset= savedContentOffset;
? ? tableView.frame= savedFrame;
? ? returnimage;
}
方法沒問題,但是一直出現(xiàn)超出屏幕部分截圖失敗問題,百度很多,都么解決
結果發(fā)現(xiàn)是調了viewDidLayoutSubviews方法,害