以编程方式快速截图 [英] Screenshot on swift programmatically

查看:117
本文介绍了以编程方式快速截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我快速按下按钮时,我正在尝试截取整个视图的屏幕截图.问题是,当我截取屏幕截图时,某些部分被切除,例如顶部...

i'm trying to take a screenshot of the whole view when i press a button in swift. The problem is that, when i take the screenshot, some parts are cut off, like the top...

其他被切除的部分是我在屏幕底部的容器视图.它包含一个开关,一个文本字段和一个按钮.这是我用来截取屏幕截图的代码...

Other part that is cut off is my container view that i have in the bottom of the screen. It contains a switch, a textfield and a button. This is the code that i'm using to take the screenshot ...

func screenShotMethod() {
    let layer = UIApplication.shared.keyWindow!.layer
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

    layer.render(in: UIGraphicsGetCurrentContext()!)
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(screenshot!, nil, nil, nil)
}

这是我用来创建容器视图的代码...

This is the code that i used to create the container view...

lazy var inputContainerView: UIView = {

    let containerView = UIView()
    containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
    containerView.backgroundColor = UIColor.white
    //Other things...

override var inputAccessoryView: UIView? {
    get {
        return inputContainerView
    }
}

override var canBecomeFirstResponder : Bool {
    return true
}

这就是结局图像的样子...

And this is what the finale image looks like...

那我该怎么解决呢? 谢谢!

So what can i do to solve it? Thanks!

推荐答案

如果您具有某种Objective-C知识,那么这里就是您想要的答案.

If you have some sort of Objective-C knowledge then here is the answer that you want.

打开此链接并遵循最后一个答案将遍历所有视图层次结构,并逐行注释以完全理解代码.

Open this Link and follow the last answer which iterates through all the view hierarchy and And have line by line comments to fully understand the code.

图像最终被转换为NSDataUIImage. 如果您仍然感到困惑,那么我可以将其转换为快捷方式,但请先自行尝试.

The Image is finally converted into both NSData and UIImage. If you are still confuse then i can convert it into swift but first try it by your self.

这是该答案的Swift代码.

Here is the Swift code of that Answer.

func screenshot() -> UIImage {
    let imageSize = UIScreen.main.bounds.size as CGSize;
    UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
    let context = UIGraphicsGetCurrentContext()
    for obj : AnyObject in UIApplication.shared.windows {
        if let window = obj as? UIWindow {
            if window.responds(to: #selector(getter: UIWindow.screen)) || window.screen == UIScreen.main {
                                // 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!
}

这篇关于以编程方式快速截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆