【问题标题】:Unable to take screenshot - screen size and return image issue无法截屏 - 屏幕尺寸和返回图像问题
【发布时间】:2017-10-05 01:13:58
【问题描述】:

我的屏幕大小等于我想要捕捉的 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)
}

【问题讨论】:

  • 为了创建自定义边界,请使用 CGSize。

标签: ios swift core-graphics screenshot arkit


【解决方案1】:

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

UIGraphicsBeginImageContextWithOptions 具有以下参数参数

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

【讨论】:

  • 再次感谢您,克鲁纳尔。对于那些希望创建自己的自定义边界的人,请像这样使用 CGSize - let screen = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 60)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2021-05-07
  • 1970-01-01
  • 2021-03-29
相关资源
最近更新 更多