从屏幕截图中排除视图

从屏幕截图中排除视图

本文介绍了从屏幕截图中排除视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我截取视图的截图:

This is how I take a screenshot of my view:

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

但是,在视图中,有一个 UIVisualEffectsView ,我想从屏幕截图中排除。

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

However, in the view, there is a UIVisualEffectsView which I'd like to exclude from the screenshot.
I tried hiding the UIVisualEffectsView just before taking the screenshot and un-hiding it afterwards but I don't want the user to see that process. (which he does if I simply hide the view because the iPad is too slow and it looks like the screen is flickering...)

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

Any ideas? Thanks in advance!

推荐答案

我会利用方法



I would take advantage of the snapshotViewAfterScreenUpdates() method

因此您可以按顺序使用它向用户显示完整未更改的视图层次结构的覆盖 UIView ,同时呈现层次结构的版本,并在其下面进行更改。

Therefore you can use it in order to display an overlay UIView of the complete un-altered view hierarchy to the user while rendering a version of the hierarchy with your changes underneath it.

唯一需要注意的是,如果您要捕获视图控制器的层次结构,则必须创建内容视图子视图,以防止在您制作的屏幕截图中呈现叠加视图层次结构的更改。然后,您需要将要渲染的视图层次结构添加到此内容视图。

The only caveat is that if you're capturing a view controller's hierarchy, you'll have to create a 'content view' subview in order to prevent the overlay view from being rendered in your screenshot where you make the changes to the hierarchy. You'll then want to add your view hierarchy that you want to render to this 'content 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 到视图的超级视图 - 而不是视图本身 - 你根本不需要弄乱层次结构。例如:

Although, if you are able to add the overlayView to the view's superview - instead of to the view itself – you don't need to mess about with the hierarchy at all. For example:

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

这篇关于从屏幕截图中排除视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:24