Swift tableview 截屏幕处理

1. 应用

通过给 tableView 添加两个扩展方法,让tableview支持截屏功能

let image = tableView.generateTableViewImage(with: tableView.getMaxHeight(with: 20)) ?? UIImage()

2. UITableView的扩展方法


extension UITableView {
    
    /// 获取最大高度
    /// - Parameters:
    ///   - maxRowCount: 最大row的count
    ///   - maxSectionCount: 最大section的count,为0表示所有
    public func getMaxHeight(with maxRowCount: Int, maxSectionCount: Int = 0) -> CGFloat {
        var sectionCount = maxSectionCount
        if maxSectionCount >= numberOfSections || maxSectionCount <= 0 {
            sectionCount = numberOfSections
        }
        var maxHeight: CGFloat = tableHeaderView?.frame.size.height ?? 0;
        for sectionIndex in 0.. maxRowCount) {
                rowCount = maxRowCount;
            }
            
            maxHeight += delegate?.tableView?(self, heightForHeaderInSection: sectionCount) ?? 0
            for rowIndex in 0.. UIImage? {
        var viewImage: UIImage?
        
        let savedContentOffset = contentOffset
        let savedFrame = frame
        
        let imageHeight = maxHeight > 0 ? maxHeight :contentSize.height
        var screensInTable = 0
        
        if (frame.size.height != 0) {
            screensInTable = Int(ceil(imageHeight / frame.size.height))
        }
        
        let sectionNum = numberOfSections
        autoreleasepool {
            let imageSize = CGSize(width: frame.size.width, height: maxHeight)
            UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
            frame = CGRect(x: 0, y: 0, width: contentSize.width, height: imageHeight)
            
            for i in 0..

你可能感兴趣的:(Swift tableview 截屏幕处理)