我如何传递 x 轴和 y 轴以及屏幕的高度和宽度以在 swift 中以编程方式截取屏幕截图

How can i pass both x and y axis along with height and width of Screen to take screenshot programmatically in swift

我在 ImageView 上添加了一个 UIImageView,所以我想截取这两个 ImageView 的屏幕截图,使它们看起来像一个,任何其他建议也将受到赞赏。

我制作了一个帮助我截屏的按钮,我添加了 x 轴和 y 轴,

func takeScreenshot(_ shouldSave: Bool = true) -> UIImage {
    var screenshotImage :UIImage?
    let layer = UIApplication.shared.keyWindow!.layer
    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(CGSize(width: 20, height: 104), false, scale);
    guard let context = UIGraphicsGetCurrentContext() else {return screenshotImage ?? UIImage(imageLiteralResourceName: "loading")}
    layer.render(in:context)
    screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    if let image = screenshotImage, shouldSave {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
    return screenshotImage ?? UIImage(named: "loading")!
}

我希望截取的屏幕截图是 imageview 的屏幕截图。附上截图,enter image description here

我相信你想说

You have 2 UIImageViews with different Images and then you want to take a screenshot of those 2 UIImageViews to make 1 image.

如果是这种情况,解决它的最简单方法是将这 2 个 UIImageView 包装在一个 UIView 中。然后将该 UIView 转换为 UIImage。因此,您可以将这 2 个 UIImageView 的屏幕截图合二为一。

In case you need code to convert UIView to UIImage:

extension UIView {
    func toImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
}

I'm not sure if this the solution to your problem.

将这两个图像视图放在一个 UIView 中,并适当地约束它们。然后截取那个 UIView 的屏幕截图。按照这个截图: