【问题标题】:How to save the whole scrollable UITableView to PNG如何将整个可滚动的 UITableView 保存为 PNG
【发布时间】:2016-02-17 12:19:47
【问题描述】:

我想将整个 UITableView 保存为 PNG 文件。

使用这段代码,我设法保存了可见部分:

UIGraphicsBeginImageContextWithOptions(tableView.layer.frame.size, false, 0.0);
tableView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let data = UIImagePNGRepresentation(image)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")
data!.writeToURL(imageURL, atomically: false)

但我也想要当前未显示的可滚动部分。

有什么想法吗?

【问题讨论】:

  • 如何截取屏幕上根本没有呈现的内容?不过很有趣。也许在一路截屏的同时以编程方式滚动它?粗制滥造是的,但这就是我所能提供的。
  • 它有多大?你能同时把它全部加载到内存中吗?
  • 我会滚动到最后一个可见单元格 + 1(如果存在)进行屏幕截图,添加到图像中,直到最后一个可见单元格 + 1 不存在为止。
  • @NSNoob,在 Android 上,您可以获得整个视图,甚至是不可见的部分。我想在 iOS 中做同样的事情
  • @Wain,是的,我可以全部加载到内存中,不会太大

标签: ios swift uitableview uiscrollview png


【解决方案1】:

这里有一些Swift extensions files,你可以添加到你的项目中来制作截图:

新代码是:

let image = tableView.screenshot
let data = UIImagePNGRepresentation(image)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")
data!.writeToURL(imageURL, atomically: false)

【讨论】:

  • 有趣,我也需要 Swift 的一样
  • @DanieleB 你看到这些类别的 [swift 等价物] 了吗? (github.com/davidman/DHSmartScreenshot/tree/master/Classes/Swift) 这个?
  • 它真的很好用!!!!我将 4 个扩展文件复制到我的项目中,然后我调用了let image = tableView.screenshot 来获取 UIImage,然后我可以将其转换为 PNG。很棒的图书馆!!!!!!
  • 我刚刚对答案进行了编辑以包含新的结果代码
猜你喜欢
  • 2011-08-03
  • 2010-10-04
  • 2011-08-08
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多