【问题标题】:Take screenshot of Visual blur view截取视觉模糊视图的屏幕截图
【发布时间】:2019-05-09 19:26:13
【问题描述】:

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

根据文档

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

所以我有以下层次结构

所以我正在拍摄完整的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()


    }

输出

任何帮助将不胜感激

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    我已经弄明白了。

    这就是我所做的

    @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 来改变它

            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)))
    

    输出

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 2012-12-20
      • 2017-05-05
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多