如何将 UI 图像添加到上下文中,以便它包含在屏幕截图中?

How to add a UI image to the context so that it gets included in the screenshot?

我有这个功能可以截取 imageView 中的内容

@IBAction func screenShotMethod(sender: UIButton) {
        //Create the UIImage

        UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0)   

        imageView.layer.renderInContext(UIGraphicsGetCurrentContext())           
        //I think here is where I could add the other imageView if I could name it properly. 

        let image = UIGraphicsGetImageFromCurrentImageContext()


    }

我想我需要这样的东西:

    UIButton(named:"monkey.png").layer.renderInContext(UIGraphicsGetCurrentContext())

在我将 imageView 添加到上下文之后,但我不确定如何正确执行此操作。如您所见,我正在尝试添加一个存在于 main.storyboard 上的名为 monkey.png 的 imageView。

有什么建议吗?

我不确定问题出在哪里,为什么您的方法对您不起作用,但是添加以下行而不是您在代码中放置的注释 ("I think here is where..."),会发生什么我了解您正在努力实现:

UIImageView(image: UIImage(named: "monkey.png")).layer.renderInContext(UIGraphicsGetCurrentContext())

所以它会让你的整个方法看起来像这样:

@IBAction func screenShotMethod(sender: UIButton) {
    UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0)
    imageView.layer.renderInContext(UIGraphicsGetCurrentContext())           
    UIImageView(image: UIImage(named: "monkey.png")).layer.renderInContext(UIGraphicsGetCurrentContext())        
    let image = UIGraphicsGetImageFromCurrentImageContext()
    // Do something with the image ;)
}