无法截图 - 屏幕尺寸和 return 图片问题

Unable to take screenshot - screen size and return image issue

我的屏幕大小与我希望捕获的 SceneView 的大小相等

let screen = CGRect(x: 0, y: 0, width: sceneView.frame.size.width, height: sceneView.frame.size.height - 60)


关于大小,我收到“无法使用类型 (CGRect) 的参数列表调用 CGFloat 类型的初始值设定项。我将如何修复此错误?

func Screenshot()
{
        UIGraphicsBeginImageContextWithOptions(UIScreen.main.bounds.size, false, CGFloat(screen))
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
}

您向 UIGraphicsBeginImageContextWithOptions 传递了错误的参数类型参数。最后一个参数是 CGFloat 类型,它接受浮点值,但您传递的是 CGRect (screen) 类型的值。

UIGraphicsBeginImageContextWithOptions 有以下参数 arguments

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)


参数

尺码
新位图上下文的大小(以磅为单位)。这表示 UIGraphicsGetImageFromCurrentImageContext 函数返回的图像的大小。要获得以像素为单位的位图大小,您必须将宽度和高度值乘以比例参数中的值。

不透明
一个布尔标志,指示位图是否不透明。如果您知道位图是完全不透明的,请指定 YES 以忽略 alpha 通道并优化位图的存储。指定 NO 意味着位图必须包含一个 alpha 通道来处理任何部分透明的像素。

规模
应用于位图的比例因子。如果指定值 0.0,比例因子将设置为设备主屏幕的比例因子。

这里是这个函数的 Apple 文档:UIGraphicsBeginImageContextWithOptions

解决方案(提示):传递值 0.0 或 1.0 而不是屏幕并查看。

UIGraphicsBeginImageContextWithOptions(UIScreen.main.bounds.size, false, CGFloat(1.0))