如何以PDF格式获取UITableView的快照 [英] How to get a snapshot of UITableView in a PDF format
本文介绍了如何以PDF格式获取UITableView的快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 UITableView
,其中填充了一些数据,但由于它包含的数据比屏幕的可视区域多,所以我只得到了它的快照,我想知道是否有任何其他方式我可以在pdf中获得整个tableview快照..这是我尝试过的感谢
I have a UITableView
which is populated with some data but since it contains data that is more cells than the viewable area of the screen, I only managed to get only the snapshot of it, I want to know if there is any other way I can get the whole tableview snapshot in pdf..this is what I have tried thanks
- (IBAction)clickMe:(id)sender
{
UIView *viewToRender = self.myTableView;
CGPoint contentOffset = self.myTableView.contentOffset;
UIGraphicsBeginImageContext(viewToRender.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// KEY: need to translate the context down to the current visible portion of the tablview
CGContextTranslateCTM(ctx, 0, -contentOffset.y);
[viewToRender.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *myImage=[[UIImageView alloc]initWithImage:image];
UIGraphicsEndImageContext();
[self createPDFfromUIViews:myImage saveToDocumentsWithFileName:@"PDF Name"];
}
- (void)createPDFfromUIViews:(UIView *)myImage saveToDocumentsWithFileName:(NSString *)string
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, myImage.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[myImage.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:string];
NSLog(@"%@",documentDirectoryFilename);
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
推荐答案
UIGraphicsBeginImageContext(self.myTableView.contentSize);
[self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
[self.myTableView.layer renderInContext:UIGraphicsGetCurrentContext()];
int rows = [self.myTableView numberOfRowsInSection:0];
int numberofRowsInView = 4;
for (int i =0; i < rows/numberofRowsInView; i++) {
[self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(i+1)*numberofRowsInView inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
[self.myTableView.layer renderInContext:UIGraphicsGetCurrentContext()];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *myImage=[[UIImageView alloc]initWithImage:image];
UIGraphicsEndImageContext();
[self createPDFfromUIViews:myImage saveToDocumentsWithFileName:@"PDF Name"];
我不知道,但这段代码对我来说就像是一个魅力..
i dont know but this code works like a charm for me..
这篇关于如何以PDF格式获取UITableView的快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文