Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式截屏图像_Ios_Swift - Fatal编程技术网

Ios 以编程方式截屏图像

Ios 以编程方式截屏图像,ios,swift,Ios,Swift,我一直被困在这样一种情况下,我希望我的代码能够截取屏幕的截图,但只能截取屏幕的中间部分 @IBAction func savePhoto(_ sender: UIButton) { // Does not go into program let size = CGSize(width: view.frame.size.width, height: view.frame.size.height) UIGraphicsBeginImageContextWithOp

我一直被困在这样一种情况下,我希望我的代码能够截取屏幕的截图,但只能截取屏幕的中间部分

    @IBAction func savePhoto(_ sender: UIButton) {

    // Does not go into program
    let size = CGSize(width: view.frame.size.width, height: view.frame.size.height)

    UIGraphicsBeginImageContextWithOptions(CGSize(width:1024.0,height: 1024.0), false, UIScreen.main.scale)


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

    UIImageWriteToSavedPhotosAlbum(newImage!, nil, nil, nil)

    }

我确实尝试过在屏幕截图时将其设置为1024x1024,我认为这会起作用,但事实并非如此。

试试这个,它对我有效。 希望对你有帮助

@IBAction func savePhoto(_ sender: UIButton) {

            UIGraphicsBeginImageContextWithOptions(CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height - 100), false, 0.0)
            self.view.drawHierarchy(in: CGRect(x: 0, y: 0, width: 1024.0, height: 1024.0), afterScreenUpdates: true)
            if let image = UIGraphicsGetImageFromCurrentImageContext() {
                UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

            }
            UIGraphicsEndImageContext();

        }

可能最简单的方法是拍摄完整的屏幕截图,然后裁剪出你想要的部分。在Swift中裁剪相对容易:谢谢,它真的很有帮助!介意解释一下self.view.drawHierarchy吗?我真的很想更多地理解这行代码!(:在此上下文中绘制视图,使屏幕截图的可见部分开始。@vserrat129