【问题标题】:Render entire UITableView as an image! to share将整个 UITableView 渲染为图像!分享
【发布时间】:2019-05-16 07:19:03
【问题描述】:

我找到了这段代码,但它节省了很多,比如 75% 的 tableView,即使是屏幕外的行,但不是完整的表格。我想知道是否有人理解它为什么不呈现完整的表格,或者如何更改以呈现完整的表格。或者其他能够可靠地呈现整个屏幕上和屏幕外 tableView 的代码? 先感谢您。希望其他人可以使用这个。我在 Swift 4 中。如果需要,我可以发布一个图像(这只是一个长表格图像,但不是完整的表格。)也许在表格完全加载之前制作表格图像不确定,猜测问题。我是编程新手。

这是我的代码:

  @objc func shareScreen() {
    UIGraphicsBeginImageContextWithOptions(CGSize(width:tableView.contentSize.width, height:tableView.contentSize.height),false, 0.0)

    let context = UIGraphicsGetCurrentContext()
    let previousFrame = tableView.frame

    tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.contentSize.width, height: tableView.contentSize.height)

    self.tableView.layer.render(in: context!)
    self.tableView.frame = previousFrame

    let image = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    self.shareCellTable(image)
}

【问题讨论】:

  • tableview的frame在它的superview的坐标空间中。重新构造它以从 0,0 扩展,其大小等于它的内容大小。
  • ok @danh 很高兴尝试,但不确定代码是否可以重新构建。如果你能帮上忙,我现在试试,然后回来报告。谢谢。
  • CGRect(x:0, y: 0:, width: tableView.contentSize.width, height: tableView.contentSize.height)
  • 我不太明白,但如果解决了我很高兴

标签: image tableview swift4 render


【解决方案1】:

这就是我所拥有的。但你我解决了它。这是我所做的: 添加类变量:

var tableHeight: CGFloat = 0.0 

在 viewWillAppear 中添加:

override func viewWillAppear() { super.viewDidLoad() 
tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil) 

在视图中将消失:

tableView.removeObserver(self, forKeyPath: "contentSize")
super.viewWillDisappear(true)

然后在shareScreen() func改变(刚刚改变;高度:tableHeight:)

tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, 
width: tableView.contentSize.width, height: tableHeight)

效果很好!我得到了整个长表作为图像:) 如果这样更好,我可以发布完整的 func 吗?这是输出的长完整表:https://imgur.com/a/iIYvVaOhappy happy ?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多