【问题标题】:Exclude View from Screenshot从屏幕截图中排除视图
【发布时间】:2016-06-28 07:36:02
【问题描述】:

这是我截取视图的方式:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

但是,在视图中,我想从屏幕截图中排除 UIVisualEffectsView
我尝试在截取屏幕截图之前隐藏UIVisualEffectsView 并在之后取消隐藏它,但我不希望用户看到该过程。 (如果我只是隐藏视图,他会这样做,因为 iPad 太慢而且看起来屏幕在闪烁......)

有什么想法吗?提前致谢!

【问题讨论】:

    标签: ios swift view screenshot


    【解决方案1】:

    也许最简单的解决方案是不要将您不需要的视图包含在截屏视图的层次结构中。你可以简单地把它放在上面。

    【讨论】:

      【解决方案2】:

      我会利用snapshotViewAfterScreenUpdates() 方法

      此方法非常有效地捕获视图的当前渲染外观并使用它来构建新的快照视图。您可以将返回的视图用作应用中当前视图的视觉替代。

      因此,您可以使用它来向用户显示完整的未更改视图层次结构的覆盖 UIView,同时呈现层次结构的一个版本以及您在其下所做的更改。

      唯一需要注意的是,如果您要捕获视图控制器的层次结构,则必须创建一个“内容视图”子视图,以防止覆盖视图在您对等级制度。然后,您需要将要呈现的视图层次结构添加到此“内容视图”。

      因此您的视图层次结构将看起来像这样:

      UIView // <- Your view
          overlayView // <- Only present when a screenshot is being taken
          contentView // <- The view that gets rendered in the screenshot
              view(s)ToHide // <- The view(s) that get hidden during the screenshot
      

      不过,如果您能够overlayView 添加到视图的超级视图(而不是视图本身)中,则根本不需要处理层次结构。例如:

      overlayView // <- Only present when a screenshot is being taken
      UIView // <- Your view – You can render this in the screenshot
          view(s)ToHide // <- The view(s) that get hidden during the screenshot
          otherViews // <- The rest of your hierarchy
      

      这样的事情应该可以达到预期的效果:

      // get a snapshot view of your content
      let overlayView = contentView.snapshotViewAfterScreenUpdates(true)
      
      // add it over your view
      view.addSubview(overlayView)
      
      // do changes to the view heirarchy
      viewToHide.hidden = true
      
      // begin image context
      UIGraphicsBeginImageContextWithOptions(contentView.frame.size, false, 0.0)
      
      // render heirarchy
      contentView.drawViewHierarchyInRect(contentView.bounds, afterScreenUpdates: true)
      
      // get image and end context
      let img = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      
      // reverse changes to the view heirarchy
      viewToHide.hidden = false
      
      // remove the overlay view
      overlayView.removeFromSuperview()
      

      【讨论】:

      • 这对@Linus-G. 有帮助吗?
      【解决方案3】:

      哦,实际上,只需遵循代码 (swift 4) 即可。也就是说,创建一个上下文,并在快照中添加您想要的项目。无需在实屏中添加视图。

      注意:currentView是快照基于的视图,一般是全屏视图。 addViews 是您要添加的视图。

      func takeSnapShot(currentView: UIView , addViews: [UIView], hideViews: [UIView]) -> UIImage {
      
      
          for hideView in hideViews {
              hideView.isHidden = true
          }
      
          UIGraphicsBeginImageContextWithOptions(currentView.frame.size, false, 0.0)
      
          currentView.drawHierarchy(in: currentView.bounds, afterScreenUpdates: true)
          for addView in addViews{
              addView.drawHierarchy(in: addView.frame, afterScreenUpdates: true)
          }
      
          let image = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
      
          for hideView in hideViews {
              hideView.isHidden = false
          }
      
          return image!
      }
      

      【讨论】:

        【解决方案4】:

        其实有一个简单的方法。

        例如,我们有一个分享按钮和在情节提要中查看,并在下面的示例代码中对其进行处理。

         @IBAction func share(_ sender: Any) {
          self.hideView.isHidden = true
          let bounds = UIScreen.main.bounds
          UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
          self.view.drawHierarchy(in: bounds, afterScreenUpdates: true)
          let image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
          //Add Activity view controller to use it
          self.hideView.isHidden = false
        }
        

        如果使用我的分享按钮截取的屏幕截图适用于我,我会使用它来隐藏广告。

        afterScreenUpdates: true 适用于在您单击按钮时隐藏以查看。它隐藏毫秒 hideView 并截取屏幕截图,然后它会重新出现在您的视图中,就像它从未隐藏一样。

        *这是我的第一个答案,希望对某人有所帮助。

        谢谢。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-05
          • 1970-01-01
          • 2021-09-24
          • 2022-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多