首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拍摄整个视图的屏幕快照(swift3)

拍摄整个视图的屏幕快照(swift3)
EN

Stack Overflow用户
提问于 2017-08-10 20:40:51
回答 1查看 343关注 0票数 0

我只想用一个按钮拍摄整个视图控制器的照片。我想我要做的是拍摄一张uiview的屏幕截图,并保存在照片库中。我要照片上的按钮。

代码语言:javascript
复制
    import UIKit

class ViewController: UIViewController {

    @IBOutlet var x: UIButton!
    var screenShot: UIImage?
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
              }

    func saveScreen() {
        self.screenShot = screenshot()
        print("cool")
    }

    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!
    }
    @IBAction func saveUIVIEW(_ sender: Any) {
        self.screenShot = screenshot()
    }

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-10 22:17:17

用这个代码。原来的答案在这里。Screenshot on swift programmatically

UIGraphicsBeginImageContextWithOptions(imageSize,-> UIImage ()->UIImage{ let imageSize = UIScreen.main.bounds.size as CGSize;UIScreen.main.bounds.size false,0) let context = UIGraphicsGetCurrentContext() for obj : AnyObject in UIApplication.shared.windows { if imageSize= obj as?UIWindow { if window.responds( to:#selector(getter: UIWindow.screen)) { //所以我们必须首先将图层的几何结构应用于图形上下文!.saveGState();//将上下文围绕在窗口的锚点上下文!.translateBy(x:window.cent.x,y: window.center .y);//应用窗口对锚点上下文的转换!.concatenate(window.transform);.translateBy(x:-window.bounds.size.width * window.layer.anchorPoint.x,y:-window.bounds.size.height * window.layer.anchorPoint.y);//将图层层次结构呈现给当前上下文window.layer.render(在: context!) //还原上下文!.restoreGState();}} let = UIGraphicsGetImageFromCurrentImageContext();返回图像!}

呼叫:

代码语言:javascript
复制
func saveScreen() {
    let image = screenshot()
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}

func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    if let error = error {
        // we got back an error!
        let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    } else {
        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    }
}

@IBAction func saveUIVIEW(_ sender: Any) {
    saveScreen()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45623100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档