【问题标题】:Swift 3 - How to take a screenshot of specific UITableView rowSwift 3 - 如何截取特定 UITableView 行的屏幕截图
【发布时间】:2017-04-13 05:59:36
【问题描述】:

我需要一个特定 UITableView 行的屏幕截图,并在屏幕截图之前删除/添加内容到单元格。

例如:单元格只包含一个文本和一个分享按钮。单击共享按钮时,我希望应用生成一张带有 text 和我的应用 logo 在顶部的图像(没有 分享按钮)。

执行此操作的最佳方法是什么?以及如何?

提前致谢。

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

@Nirav 已经在这篇文章的评论部分给了你一个提示:
How to take screenshot of portion of UIView?

所以,你现在必须在 superview 中获取单元格的 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
}

我能够获取表格视图的第 0 个单元格的快照并将其分配给 snapImageView

现在最后一步是将图像保存到相机胶卷,SO中有很多相关的帖子。

来源:Get Cell position in UITableview

添加标志和隐藏分享按钮:Saving an image on top of another image in Swift

【讨论】:

  • 谢谢,如果截图的话就解决了。但我仍然需要将应用程序徽标添加到它
  • 答案已更新,在图片上添加徽标并隐藏分享按钮
猜你喜欢
  • 1970-01-01
  • 2015-03-23
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多