将 TableView 转换为 PDF - 问题:创建新页面时最终 PDF 图像重叠

问题描述

我正在将 TableView 转换为 PDF,如下所示:

func pdfDataWithTableView(tableView: UITableView) -> Data? {
    
     let priorBounds = tableView.bounds
     
     let fittedSize = tableView.sizeThatFits(CGSize(
     width: priorBounds.size.width,height: tableView.contentSize.height
     ))
     
     tableView.bounds = CGRect(
     x: 0,y: 0,width: fittedSize.width,height: fittedSize.height
     )
     
     let pdfpageBounds = CGRect(
     x :0,width: tableView.frame.width,height: self.view.frame.height
     )
     
     let pdfData = NSMutableData()
     UIGraphicsBeginPDFContextToData(pdfData,pdfpageBounds,nil)
     
     var pageOriginY: CGFloat = 0
     while pageOriginY < fittedSize.height {
     UIGraphicsBeginpdfpageWithInfo(pdfpageBounds,nil)
     UIGraphicsGetCurrentContext()!.saveGState()
     UIGraphicsGetCurrentContext()!.translateBy(x: 0,y: -pageOriginY)
     tableView.layer.render(in: UIGraphicsGetCurrentContext()!)
     UIGraphicsGetCurrentContext()!.restoreGState()
     pageOriginY += pdfpageBounds.size.height
     tableView.contentOffset = CGPoint(x: 0,y: pageOriginY) // move "renderer"
     }
     UIGraphicsEndPDFContext()
     
     tableView.bounds = priorBounds
     var docURL = (FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)).last! as URL
     docURL = docURL.appendingPathComponent("myDocument.pdf")
     pdfData.write(to: docURL as URL,atomically: true)
     
     return pdfData as Data
}

代码运行良好;我可以将多个不同大小的单元格转换为显示在多个页面上的 pdf。

现在的问题是因为它只是在显示页面时 PDF 显示的图像被截断......下面的示例:

我正在寻找避免这种情况的解决方案...任何建议将不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)