在快照中捕获整个 tableview 单元格范围以进行打印和共享
Posted
技术标签:
【中文标题】在快照中捕获整个 tableview 单元格范围以进行打印和共享【英文标题】:Capture entire tableview cell range in snapshot for printing and sharing 【发布时间】:2020-09-04 23:42:11 【问题描述】:我正在尝试对整个滚动视图进行快照,“滚动视图”是一个表格视图,它是一个电子表格视图窗格。 SpreadsheetView 实际上是一个滚动视图/表格视图。我设置它的方式是 dequereusablecell。真的,我不知道任何其他方式,因为我对这一切都很陌生。问题是这会导致图像仅在保存的快照图像的一部分上呈现。
如何捕获整个电子表格视图/表格视图?
我已经搜索和搜索,但没有找到任何我可以理解或调整的结论性方法...
分享功能:
// Share function for share button
@objc func shareTapped()
UIGraphicsBeginImageContext(spreadsheetView.contentSize)
let savedContentOffset = spreadsheetView.contentOffset
let savedFrame = spreadsheetView.frame
spreadsheetView.contentOffset = CGPoint.zero
spreadsheetView.frame = CGRect(x: 0, y: 0, width: spreadsheetView.contentSize.width, height: spreadsheetView.contentSize.height)
spreadsheetView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
spreadsheetView.contentOffset = savedContentOffset
spreadsheetView.frame = savedFrame
UIGraphicsEndImageContext()
// Give sharing functionality
let vc = UIActivityViewController(activityItems: [image!], applicationActivities: [])
vc.excludedActivityTypes = [.assignToContact, .addToReadingList]
vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true)
电子表格视图设置:
// First initialize column and row headers & add grid data
func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell?
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: CellStyle.identifier, for: indexPath) as! CellStyle
cell.printable(with: "0")
cell.backgroundColor = .white
if case 0 = indexPath.row
cell.printable(with: columnHeaders[indexPath.section])
return cell
else if case 0 = indexPath.section
cell.printable(with: rowHeaders[indexPath.row])
return cell
// Spreadsheet setup
func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int
columnHeaders.count
func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int
rowHeaders.count
func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat
if case 0 = column
return 90
else
return 60
func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat
18
func frozenColumns(in spreadsheetView: SpreadsheetView) -> Int
1
func frozenRows(in spreadsheetView: SpreadsheetView) -> Int
1
编辑:这也不起作用:
UIGraphicsBeginImageContextWithOptions(spreadsheetView.contentSize, false, UIScreen.main.scale)
for section in 0..<spreadsheetView.numberOfColumns
for row in 0..<spreadsheetView.numberOfRows
let indexPath = IndexPath(row: row, section: section)
guard let cell = spreadsheetView.cellForItem(at: indexPath) else continue
cell.contentView.drawHierarchy(in: cell.frame, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
【问题讨论】:
【参考方案1】:由于UITableView
仅呈现可见单元格,您可以尝试创建一个与其内容具有相同框架的新表格视图,以便所有单元格都可见,但我不确定这会对性能造成多大影响。您也可以在 UIView
中构建自己的“电子表格”并对其进行快照。
【讨论】:
我认为这超出了我的知识水平:(以上是关于在快照中捕获整个 tableview 单元格范围以进行打印和共享的主要内容,如果未能解决你的问题,请参考以下文章