【问题标题】:take screen shot of the entire view (swift3) [duplicate]截取整个视图(swift 3)[重复]
【发布时间】:2018-01-19 06:10:18
【问题描述】:

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

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

}

【问题讨论】:

    标签: ios swift3 uiview save photo


    【解决方案1】:

    使用此代码。原始答案在这里。 Screenshot on swift programmatically

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

    呼叫:

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

    【讨论】:

    • 如何调用这个函数?我尝试将屏幕截图放在按钮中,但没有成功。
    • 我更新了答案。
    • 我更新了我的问题。我的代码编译没有运行时错误,但是没有任何内容保存到照片库中。
    • 我更新了答案。
    猜你喜欢
    • 2012-04-05
    • 2019-10-31
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2018-04-02
    • 2021-04-16
    相关资源
    最近更新 更多