如何截取 Scrollview 的所有内容?

时间:2022-11-15
本文介绍了如何截取 Scrollview 的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想创建一个 UIScrollView 的屏幕截图,它应该包含滚动视图的全部内容,甚至包括当前对用户不可见的内容.为此,我尝试了以下两种方法:

func snapShot(view:UIView) ->UIImage {UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0);view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true);让图像 = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();返回图像;}func snapShotScrollView(scrollView:UIScrollView) ->UIImage {让 bounds = scrollView.bounds;scrollView.bounds.size = scrollView.contentSize;让图像 = snapShot(scrollView);scrollView.bounds = 边界;返回图像;}

但生成的图像仍然只是显示滚动视图中当前对用户可见的那些视图元素.但我想查看所有视图.

我该怎么做?

编辑

我也试过了:

func 快照() ->图像?{变种图像:UIImage?UIGraphicsBeginImageContext(scrollView.contentSize)让 savedContentOffset = scrollView.contentOffset让 savedFrame = scrollView.frame;scrollView.contentOffset = CGPoint.zero;scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height);scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)图像 = UIGraphicsGetImageFromCurrentImageContext();scrollView.contentOffset = savedContentOffset;scrollView.frame = savedFrame;UIGraphicsEndImageContext();返回图片}

编辑 2

我的 UIScrollView 放置在 UIView 内,并且确实包含 UIStackView.该视图被设计为一个弹出视图,因此看起来像是弹出了一个对话.我第一次编辑的代码示例在一个空白的 UIViewController 中工作,只有一个 UIScrollView 但不在上述星座中.

解决方案

I want to create a screenshot of a UIScrollView which should contain the whole content of the scroll view, even that content, which is currently not visible to the user. For that I tried the following two methods:

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;
}

But the resulting image is still just showing those view elements inside the scroll view which are currently visible to the user. But I want to see all views.

How can I do that?

EDIT

I also tried:

func snapshot() -> UIImage? {
    var image: UIImage?

    UIGraphicsBeginImageContext(scrollView.contentSize)

    let savedContentOffset = scrollView.contentOffset
    let savedFrame = scrollView.frame;

    scrollView.contentOffset = CGPoint.zero;
    scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height);

    scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)
    image = UIGraphicsGetImageFromCurrentImageContext();

    scrollView.contentOffset = savedContentOffset;
    scrollView.frame = savedFrame;

    UIGraphicsEndImageContext();

    return image
}

Edit 2

My UIScrollView is placed inside a UIView and does contain a UIStackView. The View is designed as a popover view so that it looks like a dialogue is popping up. The code sample from my first edit is working in a blank UIViewController with only one UIScrollView but not in the mentioned constellation.

解决方案

To Swift from this answer, adding a test ViewController:

class ScreenShotTestViewController: UIViewController {

    var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView = UIScrollView(frame: CGRect(origin: CGPoint.zero, size: view.frame.size))
        scrollView.contentSize = CGSize(width: view.frame.size.width, height: view.frame.size.height * 2)
        scrollView.backgroundColor = UIColor.yellow
        view.addSubview(scrollView)

        let label = UILabel(frame: CGRect(x: 0.0, y: view.frame.size.height * 1.5, width: view.frame.size.width, height: 44.0))
        label.text = "Hello World!"
        scrollView.addSubview(label)

        let screenShot = snapshot()
    }

    func snapshot() -> UIImage?
    {      
        UIGraphicsBeginImageContext(scrollView.contentSize)

        let savedContentOffset = scrollView.contentOffset
        let savedFrame = scrollView.frame

        scrollView.contentOffset = CGPoint.zero
        scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)

        scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()

        scrollView.contentOffset = savedContentOffset
        scrollView.frame = savedFrame

        UIGraphicsEndImageContext()

        return image
    }
}

Results in an image of the complete content, including subviews:

这篇关于如何截取 Scrollview 的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Swift iOS以编程方式在导航栏下方设置滚动视图约束 下一篇:UITableView 可以在里面滚动 UICollectionView 吗?

相关文章

最新文章