iOS 13 UITextView 将 UITextView 转换为图像无法正常工作。在 iOS 12 以下运行良好

Ios*_*per 7 iphone ios swift ios13

如何在 iOS 13 中解决此问题?我正在截取 UITextView 内容大小的屏幕截图。直到 iOS 12 一切正常。但是 iOS 13 以后的问题是它没有截取完整的屏幕截图。下面是iOS 12以下和iOS 13之后的输出图片。 低于 iOS 12

iOS 13

下面是我用来截取 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()
Run Code Online (Sandbox Code Playgroud)

我得到了这个问题的解决方案 下面是 更新的工作代码

选项 1:- 下面使用 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()

}
Run Code Online (Sandbox Code Playgroud)

选项 2:- 下面使用 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

选项 3:- 尝试使用我使用过的第三方库 YYTextView https://github.com/ibireme/YYText

Cod*_*mer 2

不要绘制图层,而是使用 attributeText

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()
Run Code Online (Sandbox Code Playgroud)