首页 > 编程知识 正文

iOS 长截tableView Navigation,ios怎么长截

时间:2023-05-04 01:49:44 阅读:237375 作者:4978

iOS 长截图(tableView + Navigation) 前言效果思路遇到的问题主要代码

前言 最近有个需求就是长截屏,内容主要是tableView 及 导航栏 或其他。去网上了解了思路都是只截取了tableView的内容,然而还出现了tableView的contentSize获取不准确的问题,导致获取到的图片内容不完整,而且给的解决办法都不管用。 效果

思路 获取navigationBar的截图获取tableView的内容截图(由上至下)
2.1 先获取表头tableHeaderView 及tableFooterView内容截图
2.2 获取每个section头尾UITableViewHeaderFooterView
2.3 获取每个section的每个Cell内容截图
2.4 拼接以上内容生成一张完整的tableView的内容图navigationBar 与 tableView的内容图 ,实现拼接生成完整的图。 遇到的问题 UITableViewStyleGrouped样式

只有一组分区时
会发生
[self.tableView headerViewForSection:0]; 的值为nil 或者
[self.tableView footerViewForSection:0]; 的值为nil
暂时不知道为何?
临时解决办法
1. 现在换成 UITableViewStylePlain样式,修改区头尾悬停效果。
2. 截屏时保存tableView的contentOffset及originFrame,设置其frame为contentSize大小,截屏结束后恢复。

获取[self headerViewForSection:section] 为null

实现分区头尾时需返回UITableViewHeaderFooterView的对象,可以考虑重用,也可以直接返回。

原先没有打算存储tableView的originFrame再恢复,采用的self.tableView.contentOffset = CGPointZero和scrollToRowAtIndexPath:atScrollPosition:导致开始截屏时tableView由跳动的问题,及获取第一个分区头部时

当滑动一定距离第一个分区头部看不到的时候,[self.tableView headerViewForSection:0] == null

当时想的是
第一个方案是 self.tableView.contentOffset = CGPointZero
第二个方案是[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
这样问题是解决了,但又出现了tableView跳动的问题,吼采用存储tableView的frame 及 contentOffset ,也就是上面的第一个问题点。

主要代码 // 获取TableView 内容截图- (UIImage *)tableViewContentSizeSnapshot{ if (![self isKindOfClass:[UITableView class]]) { return nil; } NSMutableArray *screenshots = [NSMutableArray array]; // 表头 UIImage *headerScreenshot = [self screenshotOfHeaderView]; if (headerScreenshot) [screenshots addObject:headerScreenshot]; for (int section = 0; section < self.numberOfSections; section ++) { // 区头 UIImage *sectionHeaderScreenshot = [self screenshotOfHeaderViewAtSection:section]; NSLog(@"区头 = %ld image = %@",section, [self headerViewForSection:section]); if (sectionHeaderScreenshot) [screenshots addObject:sectionHeaderScreenshot]; // cell for (int row = 0; row < [self numberOfRowsInSection:section]; row ++) { NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; UIImage *cellScreenshot = [self screenshotOfCellAtIndexPath:cellIndexPath]; if (cellScreenshot) [screenshots addObject:cellScreenshot]; } // 区尾 UIImage *sectionFooterScreenshot = [self screenshotOfFooterViewAtSection:section]; if (sectionFooterScreenshot) [screenshots addObject:sectionFooterScreenshot]; } // 表尾 UIImage *footerScreenshot = [self screenshotOfFooterView]; if (footerScreenshot) [screenshots addObject:footerScreenshot]; return [UIImage verticalImageFromArray:screenshots];} // 截图- (UIImage *)screenShotWithShotView:(UIView *)shotView { UIGraphicsBeginImageContextWithOptions(shotView.bounds.size, NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); [shotView.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img;} // 拼图 由上至下+ (UIImage *)verticalImageFromArray:(NSArray *)imagesArray{ UIImage *image = nil; CGSize totalImageSize = [self verticalAppendedTotalImageSizeFromImagesArray:imagesArray]; UIGraphicsBeginImageContextWithOptions(totalImageSize, NO, 0.f); int imageOffset = 0; for (UIImage *img in imagesArray) { [img drawAtPoint:CGPointMake(0, imageOffset)]; imageOffset += img.size.height; } image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}

LHQScreenShotDemo

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。