<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          解決UITableView長截圖顯示不全問題

          共 2555字,需瀏覽 6分鐘

           ·

          2021-04-17 07:24

          作者 | 早睡早起up 
          來源 | 掘金,點擊閱讀原文查看作者更多文章

          前言

          記錄一下這個問題解決的思路,原本以為這是一個比較簡單的實現(xiàn)過程,但是我還是太年輕了,哭死?。?!總結(jié)一下:遇到困難要多百度自查,要獨立思考。

          問題

          問題描述:

          截取UITableView所有內(nèi)容并生成長圖片時,出現(xiàn)未加載的cell沒被截取到。

          問題原因:

          因為TableViewCell復(fù)用問題,當(dāng)截圖時未被截取到的cell并未被加載。

          解決

          解決思路:

          既然cell未被加載,那我們應(yīng)該先讓tableView整個全部加載出來再對其進行截圖操作,所以我先創(chuàng)建了一個scrollView,確定好顯示的內(nèi)容高度,這個高度就是tableView的內(nèi)容高度,這樣的話高度是確定了,然后我們再對scrollView進行截取內(nèi)容操作就可以了。

          代碼

          獲取tableView內(nèi)容截圖,直接調(diào)用即可。

          func getTableViewScreenshot(tableView: UITableView,whereView: UIView) -> UIImage?{
          // 創(chuàng)建一個scrollView
          let scrollView = UIScrollView()
          // 設(shè)置顏色
          scrollView.backgroundColor = UIColor.white
          // 設(shè)置位置
          scrollView.frame = whereView.bounds
          // 設(shè)置滾動位置
          scrollView.contentSize = CGSize(width: screenWidth, height: tableView.contentSize.height)
          // 將tableView加載到視圖中
          scrollView.addSubview(tableView)
          // 設(shè)置位置
          tableView.snp.makeConstraints { (make) in
          make.top.left.right.equalTo(scrollView)
          make.width.equalTo(screenWidth)
          make.height.equalTo(tableView.contentSize.height)
          }
          /// 添加到指定視圖
          whereView.addSubview(scrollView)
          /// 截圖
          let image = snapshotScreen(scrollView: scrollView)
          /// 移除scrollView
          scrollView.removeFromSuperview()
          return image
          }

          以下為截圖方法

          func snapshotScreen(scrollView: UIScrollView) -> UIImage?{
          if UIScreen.main.responds(to: #selector(getter: UIScreen.scale)) {
          UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false, UIScreen.main.scale)
          } else {
          UIGraphicsBeginImageContext(scrollView.contentSize)
          }

          let savedContentOffset = scrollView.contentOffset
          let savedFrame = scrollView.frame
          let contentSize = scrollView.contentSize
          let oldBounds = scrollView.layer.bounds

          if #available(iOS 13, *) {
          //iOS 13 系統(tǒng)截屏需要改變tableview 的bounds
          scrollView.layer.bounds = CGRect(x: oldBounds.origin.x, y: oldBounds.origin.y, width: contentSize.width, height: contentSize.height)
          }
          //偏移量歸零
          scrollView.contentOffset = CGPoint.zero
          //frame變?yōu)閏ontentSize
          scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)

          //截圖
          if let context = UIGraphicsGetCurrentContext() {
          scrollView.layer.render(in: context)
          }
          if #available(iOS 13, *) {
          scrollView.layer.bounds = oldBounds
          }
          let image = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
          //還原frame 和 偏移量
          scrollView.contentOffset = savedContentOffset
          scrollView.frame = savedFrame
          return image
          }





          瀏覽 128
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  思思精品热视频 | 国产成人无码精品亚洲 | 高清无码在线视频 | 美女被干操的视频网站 | 国产1区在线观看 |