gpt4 book ai didi

ios - 截取视觉模糊 View 的屏幕截图

转载 作者:行者123 更新时间:2023-11-29 05:46:53 26 4
gpt4 key购买 nike

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

根据文档

Many effects require support from the window that hosts the UIVisualEffectView. Attempting to take a snapshot of only the UIVisualEffectView will result in a snapshot that does not contain the effect. To take a snapshot of a view hierarchy that contains a UIVisualEffectView, you must take a snapshot of the entire UIWindow or UIScreen that contains it.

所以我有以下层次结构

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

如有任何帮助,我们将不胜感激

最佳答案

我已经弄清楚了。

这就是我所做的

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

输出

enter image description here

关于ios - 截取视觉模糊 View 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56057953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com