简体   繁体   中英

Take Screenshot Current View (Including Keyboard) Swift 4

My scenario:

I have a UIViewController with UIButton and a UITextField being a first responder, when the UIButton is clicked, I present another view controller using a custom transition (this custom transition delegates to a UIPreentationController which adds a "dimmed" view (black background with alpha 0.5) to the containerView bounds and another view at the bottom with 200 points height to show a message. The whole thing to show an alert.

I tried MANY things to keep the keyboard from the previous screen being dismissed during/after this transition without any luck, it always disappear. Now I would like to know if it is possible to take a screenshot of the screen including the keyboard, right after the textfield being the responder? Without requesting additional app capabilities. Then I could add this image to the background during my transition and keep the "frozen" screen aspect.

Thank you for any direction

EDIT :

I found the following code with codes what I want. However, the keyboard background is being lost. Does anybody know why?

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

In this way there will be also the keyboard.

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

If you want use that for a custom transition and you need the UIImage , you could easily change the code like that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM