以编程方式加载 Web 视图并拍摄快照以显示在集合视图中



我目前正在加载collectionView WebViews,这还可以,但效果不佳,例如广告经常掩盖webview的内容。 我想做的是以编程方式加载webview并拍摄它的快照,然后用图像而不是WebViews本身重新加载collectionView

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ResultsCollectionViewCell
        if let url = Urls?[indexPath.row] {
            self.activityIN.stopAnimating()
            self.progressLabel.removeFromSuperview()
            cell.webView.loadRequest(URLRequest(url: url))
        }

        return cell
    }
  • 创建 2 个单元格 - 一个具有 Web 视图,另一个具有图像视图
  • 重新加载时,确定是否仍然存在屏幕截图并使用ImageView显示单元格
  • 在 Web 视图中使用自定义 Web 视图,该视图在完成加载后保存 拍摄快照,保存屏幕截图,将屏幕截图名称添加到 Urls 集合,然后删除 Web 视图并将其替换为图像视图

最新更新