我有一个scrollview,我需要该滚动视图的完整内容的屏幕截图,我在stackoverflow上搜索并尝试了许多解决方案但是只提供了viewcontroller总高度的屏幕截图。 例如,如果scrollview的高度为1096,Viewcontroller高度为532,则根据Viewcontroller高度截取内容 我发现只有这个代码在stackoverflow上有用,它返回了scrollview的完整高度,但它清除了Viewcontroller中scrollview的内容。任何帮助如何保留scrollview的内容或任何更好的解决方案,以获取swift中的完整屏幕截图。谢谢你
<Style TargetType="ToggleButton" x:Key="BtnKey">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<ControlTemplate.Resources>
<Storyboard x:Key="StartAni">
<DoubleAnimation
Storyboard.TargetName="border2"
Storyboard.TargetProperty="StrokeThickness" To="0" Duration="0:0:0.2"/>
<ColorAnimation
Storyboard.TargetName="border2"
Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
To="Red" Duration="0"/>
<DoubleAnimation
Storyboard.TargetName="border3"
Storyboard.TargetProperty="StrokeThickness"
From="0.0" To="5.0" Duration="0"/>
</Storyboard>
<Storyboard x:Key="StopAni">
<DoubleAnimation
Storyboard.TargetName="border2"
Storyboard.TargetProperty="StrokeThickness" To="5" Duration="0:0:2.5"/>
<ColorAnimation
Storyboard.TargetName="border2"
Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
To="LightGreen" Duration="0"/>
<DoubleAnimation
Storyboard.TargetName="border3"
Storyboard.TargetProperty="StrokeThickness"
From="5.0" To="0.0" Duration="0:0:0.2"/>
</Storyboard>
</ControlTemplate.Resources>
<Grid>
<Canvas>
<Ellipse x:Name="border1" Width="100" Height="100" Stroke="Transparent"/>
<Ellipse x:Name="border2" Width="90" Height="90" Stroke="Transparent"/>
<Ellipse x:Name="border3" Width="50" Height="50" Stroke="Transparent"/>
</Canvas>
<ContentPresenter />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsDone}" Value="false">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource StartAni}">
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IsDone}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource StopAni}">
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
答案 0 :(得分:1)
在我的指导下使用this,我认为您可以使用以下方式对任何视图进行快照:
func snapShot(view:UIView) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
虽然使用滚动视图,但这只会返回滚动视图框架中可见内容的图像,因此我们可以创建辅助函数:
func snapShotScrollView(scrollView:UIScrollView) -> UIImage
{
let bounds = scrollView.bounds
scrollView.bounds.size = scrollView.contentSize
let image = snapShot(scrollView)
scrollView.bounds = bounds
return image
}
这将简要调整滚动视图对其内容大小的界限。
我不知道这段代码是多么高效,但这对我来说非常适合在快捷的游乐场玩。例如,运行此代码:
let scrollView = UIScrollView(frame:CGRect(x: 0.0, y: 0.0, width: 200.0, height: 200.0))
scrollView.backgroundColor = UIColor.whiteColor()
scrollView.contentSize = CGSize(width: 600.0, height: 600.0)
let blueView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 200.0))
blueView.backgroundColor = UIColor.blueColor()
let redView = UIView(frame: CGRect(x: 200.0, y: 200.0, width: 400.0, height: 600.0))
redView.backgroundColor = UIColor.redColor()
scrollView.addSubview(blueView)
scrollView.addSubview(redView)
let image = snapShotScrollView(scrollView)