【问题标题】:Take Screenshot Current View (Including Keyboard) Swift 4截屏当前视图(包括键盘)Swift 4
【发布时间】:2020-03-27 19:28:50
【问题描述】:

我的场景:

我有一个 UIViewController 和一个 UIButton 和一个 UITextField 作为第一响应者,当单击 UIButton 时,我使用自定义转换呈现另一个视图控制器(这个自定义转换委托给 UIPreentationController它将“暗淡”视图(alpha 0.5 的黑色背景)添加到 containerView 边界,并在底部添加另一个视图,高度为 200 点以显示消息。整个显示警报。

我尝试了很多方法来保持键盘在此转换期间/之后被关闭,但没有任何运气,它总是消失。现在我想知道是否可以在文本字段作为响应者之后截取包括键盘在内的屏幕截图?无需请求额外的应用程序功能。然后我可以在过渡期间将此图像添加到背景中,并保持“冻结”的屏幕外观。

感谢您的任何指导

编辑

我找到了以下代码,其中包含我想要的代码。但是,键盘背景正在丢失。有人知道为什么吗?

func screenshot() -> UIImage {
    let imageSize = UIScreen.main.bounds.size as CGSize;
    UIGraphicsBeginImageContextWithOptions(imageSize, true, 0)
    let context = UIGraphicsGetCurrentContext()
    for obj : AnyObject in UIApplication.shared.windows {
        if let window = obj as? UIWindow {
            print(window)
            if window.responds(to: #selector(getter: UIWindow.screen)) || window.screen == UIScreen.main {

                print(window)
                // so we must first apply the layer's geometry to the graphics context
                context!.saveGState();
                // Center the context around the window's anchor point
                context!.translateBy(x: window.center.x, y: window.center
                    .y);
                // Apply the window's transform about the anchor point
                context!.concatenate(window.transform);
                // Offset by the portion of the bounds left of and above the anchor point
                context!.translateBy(x: -window.bounds.size.width * window.layer.anchorPoint.x,
                                     y: -window.bounds.size.height * window.layer.anchorPoint.y);

                // Render the layer hierarchy to the current context
                window.layer.render(in: context!)

                // Restore the context
                context!.restoreGState();
            }
        }
    }
    let image = UIGraphicsGetImageFromCurrentImageContext();

    return image!
}

https://giphy.com/gifs/XBLtqJZHoPOgvSgrV3

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    这样,键盘也就有了。

    private func takeScreenshot() {
        let snaposhotView: UIView = UIScreen.main.snapshotView(afterScreenUpdates: false)
        let renderer: UIGraphicsImageRenderer = UIGraphicsImageRenderer(size: snaposhotView.bounds.size)
        let image: UIImage = renderer.image { context in
                snaposhotView.drawHierarchy(in: snaposhotView.bounds, afterScreenUpdates: true)
        }
        // To save in Photos
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
    

    如果您想将其用于自定义转换并且需要UIImage,您可以轻松地更改代码:

    private func takeScreenshot() -> UIImage {
        let snaposhotView: UIView = UIScreen.main.snapshotView(afterScreenUpdates: false)
        let renderer: UIGraphicsImageRenderer = UIGraphicsImageRenderer(size: snaposhotView.bounds.size)
        return renderer.image { context in
                snaposhotView.drawHierarchy(in: snaposhotView.bounds, afterScreenUpdates: true)
        }
    }
    

    【讨论】:

      【解决方案2】:
      func screenShotMethod() {
              let layer = UIApplication.sharedApplication().keyWindow!.layer
              let scale = UIScreen.mainScreen().scale
              UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
      
              layer.renderInContext(UIGraphicsGetCurrentContext()!)
              let screenshot = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
      
              UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
          }
      

      【讨论】:

      • 我将您的代码修复为 Swift 4,但仍然......它不包括键盘。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2018-10-20
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多