ios - 如何获取 UITextView 的完整快照
问题描述
我正在尝试UITextView
使用以下代码拍摄快照
extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.frame, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
但我给了我一半的快照。甚至UITextView
和Snapshot
图像相同width
,height
当我使用print
. 我也尝试了不同的代码,但没有奏效。谁能告诉我如何修复它,我想要完整的快照UITextView
。
这是我的UITextView
With Frame
(0.0, 97.0, 414.0, 234.0)
这是使用pb_takeSnapshot()
With Frame
width = 414.0, height = 234.0
的快照图像。
解决方案
您不应该使用frame
来绘图(出于某种原因,正如您在评论中所说),您bounds
没有给出正确的值。
在不查看视图层次结构的情况下很难调试原因,但是您仍然应该能够创建正确的快照。
创建矩形以从头开始绘制:
let rect = CGRect(x: 0.0, y: 0.0, width: bounds.size.width, height: bounds.size.height)
drawHierarchy(in: rect, afterScreenUpdates: true)
推荐阅读
- c# - 方法中无法识别名称
- python-3.x - 从python文件打印到kivy
- android - 未知属性 android:layout ,XML android 中没有自动建议
- scala - 类需要是抽象的,因为它的成员没有被实例化
- mysql - Column count doesn't match value count at row 1 in MySQL
- android-studio - Saving Bitmap without Losing Quality (Gradient background)
- ios - UIImage is not showing after selecting image from gallery or camera
- node.js - Hyperledger 结构:错误:链码参数错误:json:无法将数组解组为字符串类型的 Go 结构字段 strArgs.Args
- c++ - 用户输入数组大小(N)。从 0 到 N 的数字填充数组
- c# - Get specific columns in UnitOfWork and generic repository in Entity Framework