拍摄视觉模糊视图的屏幕截图

时间:2019-05-09 11:02:26

标签: ios swift

我正在尝试截取视觉模糊视图的屏幕截图。

根据文档

  

许多效果需要宿主UIVisualEffectView的窗口的支持。尝试仅拍摄UIVisualEffectView的快照将导致不包含效果的快照。要拍摄包含UIVisualEffectView的视图层次结构的快照,必须拍摄包含UIVisualEffectView的整个UIWindow或UIScreen的快照。

所以我有以下等级

enter image description here

因此,我正在拍摄完整的UIView屏幕截图,并尝试裁剪viewScreenShot的矩形,但我无法做到这一点

这是我到目前为止尝试过的

 @IBAction func btnTapped(_ sender: Any) {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)

    //    self.viewScreenShot.layer.render(in: UIGraphicsGetCurrentContext()!)
        self.view.drawHierarchy(in: CGRect(x: view.frame.origin.x, y: view.frame.origin.y, width: view.frame.width, height: view.frame.height), afterScreenUpdates: true)

        let image = UIGraphicsGetImageFromCurrentImageContext()

        let cgIImage =  image?.cgImage?.cropping(to: CGRect(origin: viewScreenShot.frame.origin, size: viewScreenShot.frame.size))

        let newImage = UIImage(cgImage: cgIImage!, scale: 1.0, orientation: UIImage.Orientation.up)

        (self.view.viewWithTag(90) as? UIImageView)?.image = newImage

        UIGraphicsEndImageContext()


    }

输出

enter image description here

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我知道了。

这就是我所做的

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css">


<div class="row">
  <div class="col-md-6 float-left">
    <p>Funds (Add/Reduce)</p>
  </div>
  <div class="col-md-6 mb-3 pl-0 float-left ac-set">
    <div class="input-group add-reduce-div">
      <div class="input-group-prepend">
        <div class="dropdown">
          <button class="btn btn-dropdown dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
          <div class="dropdown-menu add-reduce" aria-labelledby="dropdownMenuButton">
            <a class="dropdown-item" href="#"><i class="fas fa-plus funds-icons"></i>&nbsp; Add</a>
            <a class="dropdown-item" href="#"><i class="fas fa-minus funds-icons"></i>&nbsp; Reduce</a>
          </div>
        </div>
      </div>
      <input type="number" class="form-control universal-input add-reduce-input">
      <div class="input-group-append">
        <button type="button" class="btn btn-dropdown" disabled="disabled">Add</button>
      </div>
    </div>
  </div>
</div>

有两个问题是由于规模引起的

所以

我替换了

@IBAction func btnTapped(_ sender: Any) {

        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)

    //    self.viewScreenShot.layer.render(in: UIGraphicsGetCurrentContext()!)
        self.view.drawHierarchy(in: CGRect(x: view.frame.origin.x, y: view.frame.origin.y, width: view.frame.width, height: view.frame.height), afterScreenUpdates: true)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        // FULL


        let cgIImage =  image?.cgImage?.cropping(to: CGRect(origin: CGPoint(x: viewScreenShot.frame.origin.x * UIScreen.main.scale  , y: viewScreenShot.frame.origin.y * UIScreen.main.scale), size: CGSize(width: viewScreenShot.bounds.width * UIScreen.main.scale, height: viewScreenShot.bounds.height * UIScreen.main.scale)))

        let newImage = UIImage(cgImage: cgIImage!, scale: UIScreen.main.scale, orientation: UIImage.Orientation.up)

        (self.view.viewWithTag(90) as? UIImageView)?.image = newImage

        UIGraphicsEndImageContext()


    }

    let newImage = UIImage(cgImage: cgIImage!, scale: 1.0, orientation: UIImage.Orientation.up)

通过乘以 let newImage = UIImage(cgImage: cgIImage!, scale: UIScreen.main.scale, orientation: UIImage.Orientation.up)

进行更改
UIScreen.main.scale

输出

enter image description here

相关问题