gpt4 book ai didi

ios - 从屏幕截图中排除 View

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:47 24 4
gpt4 key购买 nike

这是我截取 View 的方式:

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

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

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

最佳答案

我会利用 snapshotViewAfterScreenUpdates()方法

This method very efficiently captures the current rendered appearance of a view and uses it to build a new snapshot view. You can use the returned view as a visual stand-in for the current view in your app.

因此,您可以使用它来向用户显示完整的未更改 View 层次结构的覆盖层 UIView,同时呈现层次结构的一个版本,其中包含您的更改。

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

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

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 添加到 View 的父 View - 而不是添加到 View 本身 - 您就不需要弄乱层次结构根本。例如:

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

关于ios - 从屏幕截图中排除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35967638/

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