[英]Swift 3 - How to take a screenshot of specific UITableView row
I need to have a screenshot of a specific UITableView row and remove/add content to the cell before the screenshot. 我需要具有特定UITableView行的屏幕截图,并在屏幕截图之前删除/向单元格添加内容。
For example: the cell contains only a text and a share button. 例如:单元格仅包含一个文本和一个共享按钮。 On share button click, I want the app to generate an image with the text and my app logo on the top ( without the share button). 单击共享按钮时,我希望该应用程序生成一个图像,其中文本和我的应用程序徽标位于顶部( 没有 共享按钮)。
What is the best approach to do this? 最好的方法是什么? and how? 如何?
Thanks in advance. 提前致谢。
@Nirav already gave you a hint in the comment section with this post: @Nirav已经在这篇文章的评论部分为您提供了提示:
How to take screenshot of portion of UIView? 如何获取部分UIView的屏幕截图?
So, you have now to get the Rect of the cell in the superview, so i made a sample action method: 因此,您现在必须在超级视图中获取单元格的Rect,因此我制作了一个示例操作方法:
@IBOutlet var snapImageView: UIImageView!
@IBAction func snapshotrow(_ sender: Any) {
//get the cell
if let cell = myTable.cellForRow(at: IndexPath(row: 0, section: 0)) as? MyCustomCellClass {
//hide button
cell.shareButton.isHidden = true
let rectOfCellInTableView = myTable.rectForRow(at: IndexPath(row: 0, section: 0))
let rectOfCellInSuperview = myTable.convert(rectOfCellInTableView, to: myTable.superview)
snapImageView.image = self.view.snapshot(of: rectOfCellInSuperview)
let finalImage = saveImage(rowImage: snapImageView.image)
//test final image
snapViewImage.image = finalImage
}
}
func saveImage(rowImage: UIImage) -> UIImage {
let bottomImage = rowImage
let topImage = UIImage(named: "myLogo")!
let newSize = CGSize.init(width: 41, height: 41) // set this to what you need
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
bottomImage.draw(in: CGRect(origin: .zero, size: newSize))
topImage.draw(in: CGRect(origin: .zero, size: newSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
and I was able to get the snapshot of the 0th cell of my table view and assigned it to the snapImageView
. 并且我能够获取表视图的第0个单元格的快照并将其分配给snapImageView
。
Now the last step is to save the image to the camera roll, there are lots of related post out there in SO. 现在,最后一步是将图像保存到相机胶卷中,因此有很多相关的文章。
Source: Get Cell position in UITableview 来源: 获取单元格在UITableview中的位置
Adding logo and hiding share button: Saving an image on top of another image in Swift 添加徽标并隐藏共享按钮: 在Swift中将图像保存在另一个图像之上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.