ios - 为什么截图图像被拉伸了?
问题描述
我正在使用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()
解决方案
您将图像上下文设置为 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()
推荐阅读
- spring - 在使用 @WebFluxTests 测试的控制器中使用 WebClient 会引发 java.lang.IllegalArgumentException:URI 不是绝对的
- terraform - terraform 中的动态地图创建
- django - Django Pagination - 对象列表不在模板中循环
- php - WP Fullcalendar 中未显示选定日期 ACF 多日期字段
- ios - 在 Swift 中从看起来像对象数组的字符串中分离键值
- java - 如何使用不同的参数类型保持相同的函数
- reactjs - 如何在 video.js 中的新创建元素中使用“This.props”在反应中
- semantic-ui - 像侧边栏一样使用语义 UI 菜单
- c - 在 PCIe 中写入持久内存
- python - 如何在 Windows 10 上使用 Nvidia George GTX 1650 在 Anaconda 上安装 Tensorflow-gpu 2.2