self.tableView.userInteractionEnabled =NO;//暂时禁用交互,让tableView自己滑动
// 生成保存的单个图片先存入array
NSMutableArray * imageArr = [NSMutableArrayarray];
// 将tableView滚动至第一个cell的位置,确保第一组cell全部可见
[self.tableViewsetContentOffset:CGPointMake(0,150) animated:NO];
for (NSInteger i =0; i < 6; i++) {
NSIndexPath * indexPath = [NSIndexPathindexPathForRow:i inSection:0];
// 将cell一个一个渲染成图片
LJFallDownSectionOneCell * cell = [self.tableViewcellForRowAtIndexPath:indexPath];
UIGraphicsBeginImageContext(cell.frame.size);
[cell.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage * image =UIGraphicsGetImageFromCurrentImageContext();
if (image) {
[imageArr addObject:image];
}
UIGraphicsEndImageContext();
LJLog(@"aaa%ld",imageArr.count);
}
// 第二组全部可见后开始渲染
[UIViewanimateWithDuration:3.0animations:^{
[self.tableViewscrollToRowAtIndexPath:[NSIndexPathindexPathForRow:0inSection:1]atScrollPosition:UITableViewScrollPositionTopanimated:NO];// animated 必须为NO,否则和UIView animateWithDuration冲突
} completion:^(BOOL finished) {//动画完成后开始渲染
if (finished) {
for (NSInteger j =0; j < 10; j ++) {
NSIndexPath * indexPath = [NSIndexPathindexPathForRow:j inSection:1] ;
LJFallDownSectionOneCell * cell = [self.tableViewcellForRowAtIndexPath:indexPath];
UIGraphicsBeginImageContext(cell.frame.size);
[cell.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage * image =UIGraphicsGetImageFromCurrentImageContext();
if (image) {
[imageArr addObject:image];
}
UIGraphicsEndImageContext();
LJLog(@"bbb%ld",imageArr.count);
}
// start
//将所有小图片拼成一张大图片
// 先计算出大图片的大小
CGFloat totalH = 0;
for (UIImage * imagein imageArr) {
totalH += image.size.height;
}
UIGraphicsBeginImageContext(CGSizeMake(LFScreenWidth, totalH));
CGFloat hight = 0;
for (NSInteger i =0; i < imageArr.count; i ++) {
UIImage * image = imageArr[i];
[image drawInRect:CGRectMake(0, hight,LFScreenWidth, image.size.height)];
hight += image.size.height;
}
UIImage * bigImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/// 存入沙盒
NSData * imageData = UIImagePNGRepresentation(bigImage);
NSString * filePath =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)[0];
NSString * imagePath = [filePathstringByAppendingString:@"/history.png"];
BOOL flag= [imageData writeToFile:imagePath atomically:YES];
if (flag) {
LJLog(@"存入图片成功");
}else{
LJLog(@"存入图片失败");
}
[MBProgressHUD showSuccess:@"保存成功"toView:self.view];
// end
}
}];
self.tableView.userInteractionEnabled =YES;