0
我正在用一段简单的代码挣扎,我已经花了很多时间寻找这个,检查论坛并尝试了不同的东西。我几乎在那里,但不完全。基本上我需要将UITableView的内容转换为PDF。Convertir UITableView to PDF
下面的代码似乎正常工作,除非用户已将划过表格内容的末尾,否则该部分将不会呈现。我以编程方式滚动了UITableView,但似乎并没有这样做。我越来越多地进入Quartz2D进一步了解这个东西,但客户需要这个固定昨日:-)
-(void) createPDFfromUITable:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
CGRect priorBounds = self.tableView.bounds;
CGSize fittedSize = [self.tableView sizeThatFits:CGSizeMake(priorBounds.size.width,3000)];
self.tableView.bounds = CGRectMake(0, 0, fittedSize.width,1800);
CGRect pdfPageBounds = CGRectMake(0, 0,self.view.bounds.size.width,792);
NSMutableData *pdfData = [[NSMutableData alloc] init];
UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil);
CGFloat pageOriginY=0;
for(int i=0;i<2;i++)//for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height)
{
UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil);
CGContextSaveGState(UIGraphicsGetCurrentContext());
{
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
[self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:([_resultados count]-1)] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
} CGContextRestoreGState(UIGraphicsGetCurrentContext());
pageOriginY += pdfPageBounds.size.height;
}
UIGraphicsEndPDFContext();
self.tableView.bounds = priorBounds; // Reset the tableView
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePathPDF = [documentsPath stringByAppendingPathComponent:aFilename];
[pdfData writeToFile:filePathPDF atomically:YES];
[self showEmail:filePathPDF];
}
如果您有任何建议固定我现在使用的,你碰巧有码整个片段完成整个事情,这将非常感激。