How to render a whole UITableView as an UIImage in iOS?

后端 未结 13 2167
太阳男子
太阳男子 2020-12-17 23:54

I have simple UITableView with some data. The table\'s height is greater than the screen size. Now I need to catch screenshot of this table (a whole table). I k

13条回答
  •  太阳男子
    2020-12-18 00:34

    Swift 4 version of Parth Bhadaja's answer:

    func generateImage(tableView:UITableView) -> UIImage {
        UIGraphicsBeginImageContext(tableView.contentSize);
        tableView.scrollToRow(at: NSIndexPath(row: 0, section: 0) as IndexPath, at: UITableViewScrollPosition.top, animated: false)
        tableView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let row = tableView.numberOfRows(inSection: 0)
        let numberofRowthatShowinscreen = 4
        let scrollCount = row / numberofRowthatShowinscreen
    
        for i in 0..

    Should be noted that this function does return an image of the whole tableView, even cells which haven't been loaded onto the screen yet.

提交回复
热议问题