iOS 13 UITextView将UITextView转换为图像无法正常工作.在低于iOS 12的环境下工作正常 [英] iOS 13 UITextView converting UITextView to image is not working properly. Working fine in below iOS 12
问题描述
如何在iOS 13中解决此问题? 我正在截取UITextView内容大小的屏幕截图.直到iOS 12一切正常.但是从iOS 13开始的问题是,它并没有截取完整的屏幕截图.以下是低于iOS 12和低于iOS 13的输出图片. 少于iOS 12
How to solve this issue in iOS 13? I am taking screenshot of UITextView content size..Till iOS 12 everything is working fine. But issue with iOS 13 onwards it's not taking full screenshot it's cutting. Below is output picture for less than iOS 12 and after iOS 13. Less than iOS 12
下面是我用来拍摄UITextView屏幕截图的代码:
Below is code I'm using to take screenshot of UITextView:
let textView = UITextView()
UIGraphicsBeginImageContextWithOptions(textView.contentSize, textView.isOpaque, 0.0)
let savedContentOffset: CGPoint = textView.contentOffset
let savedFrame: CGRect = textView.frame
textView.frame = CGRect(x: 0, y: 0, width: textView.contentSize.width, height: textView.contentSize.height)
textView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
textView.contentOffset = savedContentOffset
textView.frame = savedFrame
UIGraphicsEndImageContext()
我已找到此问题的解决方案 下边是 更新后的工作代码
I got the solution for this issue Below is UPDATED WORKING CODE
选项1:- 下面的代码使用UILabel
OPTION 1:- Below code using UILabel
let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
func buttonAction() {
let textView = UITextView()
textView.frame = CGRect(x: 10, y: 50, width: SCREEN_WIDTH - 20, height: SCREEN_HEIGHT * 2)
let lab = UILabel(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH - 20, height: textView.contentSize.height))
lab.backgroundColor = UIColor.white
lab.font = textView.font
lab.textColor = UIColor.black
lab.numberOfLines = 0
lab.text = textView.text
textView.addSubview(lab)
UIGraphicsBeginImageContextWithOptions(CGSize(width: SCREEN_WIDTH - 20, height: textView.contentSize.height), _: true, _: 1)
if let context = UIGraphicsGetCurrentContext() {
lab.layer.render(in: context)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
textView.frame = CGRect(x: 10, y: 50, width: SCREEN_WIDTH - 20, height: SCREEN_HEIGHT * 2)
lab.removeFromSuperview()
}
选项2:- 下面的代码使用TempView
OPTION 2:- Below code using TempView
let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
func buttonAction() {
let textView = UITextView()
textView.frame = CGRect(x: 10, y: 50, width: SCREEN_WIDTH - 20, height: SCREEN_HEIGHT * 2)
UIGraphicsBeginImageContextWithOptions(CGSize(width: SCREEN_WIDTH - 20, height: textView.contentSize.height), _: true, _: 1)
if #available(iOS 13, *) {
// iOS 13 (or newer)
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: textView.contentSize.width, height: textView.contentSize.height))
tempView.addSubview(textView)
if let context = UIGraphicsGetCurrentContext() {
tempView.layer.render(in: context)
}
tempView.removeFromSuperview()
view.addSubview(textView)
} else {
// iOS 12 or older
if let context = UIGraphicsGetCurrentContext() {
textView.layer.render(in: context)
}
}
选项3:- 尝试使用我已经使用YYTextView的第三方库 https://github.com/ibireme/YYText
OPTION 3:- Try to use third party library i have used YYTextView https://github.com/ibireme/YYText
推荐答案
不要绘制图层,而应使用attributedText
Don't draw the layer, use the attributedText instead
let size = self.textView.contentSize
UIGraphicsBeginImageContextWithOptions(size, self.textView.isOpaque, 0)
//the textView has 8 margin
textView.attributedText.draw(in: CGRect.init(x: 0, y: 0, width: size.width - 16, height: size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
这篇关于iOS 13 UITextView将UITextView转换为图像无法正常工作.在低于iOS 12的环境下工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!