是否可以从UITableView生成PDF文件? [英] If it's possible to generate a PDF file from a UITableView?
问题描述
我的应用程序有一个带有自定义单元格(包括文本字段,标签和按钮)的表格视图,我想从该表格视图中生成一个pdf文件.
My application has a tableview with custom cells(include textfield, label and button), I wanna generate a pdf file from the tableview.
下面是代码(现在假设表格视图的内容都是可见的):
Bellow is the code(now assume the tableview's content is all visible):
UIGraphicsBeginImageContext(self._tableView.bounds.size);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, self._tableView.bounds.size.width, self._tableView.bounds.size.height), nil);
// what's the code here?
// [self._tableView drawInRect:];
UIGraphicsEndPDFContext();
我总是生成一个空白的pdf文件. 当前,我要做的是将tableview生成为图像,然后将图像绘制到当前上下文,然后将得到pdf文件.但是有什么好主意吗?
I always generate an blank pdf file. Currently what I do is to generate the tableview as a image, then draw the image to the current context, then will get the pdf file. but any nice idea?
推荐答案
HI,您可以通过以下代码将当前视图转换为图像,然后必须使用该图像通过
HI , you can convert current view as image through the follwoing code and then you have to use that image to create PDF File through the link
- (UIImage *)captureView:(UIView *)view {
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[view.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
这篇关于是否可以从UITableView生成PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!