【问题标题】:Why screenshot image stretched?为什么截图图像被拉伸了?
【发布时间】:2018-08-30 13:21:27
【问题描述】:

我正在使用 UIGraphicsBeginImageContextWithOptions 和自定义 CGSize 我不知道为什么我得到拉伸图像?

UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width, height: 200) , false, 0)
let cellRect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200)
view.drawHierarchy(in: cellRect, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()//stretched image
UIGraphicsEndImageContext()

【问题讨论】:

  • 您能附上“拉伸图像”吗?您是否在“照片”应用中看到它被拉伸了?或在您的应用程序的其他地方? (似乎你在代码方面做的一切都是正确的)

标签: ios swift uiimage xcode9


【解决方案1】:

您将图像上下文设置为 200 的高度,但随后您对单元格 rect 执行相同操作,这意味着它将截取整个屏幕截图并将其放入 (screenWidth, 200) 帧中。您需要保留 200 的上下文,然后将 view.bounds 用于 cellRect,如果您需要,您应该获得视图的顶部 200 px

UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width, height: 200) , false, 0)
let cellRect = self.view.bounds
self.view.drawHierarchy(in: cellRect, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()//stretched image
UIGraphicsEndImageContext()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 2018-01-12
  • 2013-01-21
  • 1970-01-01
  • 2011-06-22
相关资源
最近更新 更多