gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:00:08 26 4
gpt4 key购买 nike

如何在 iOS 13 中解决此问题?我正在截取 UITextView 内容大小的屏幕截图。直到 iOS 12 一切正常。但 iOS 13 以后的问题是,它无法截取正在剪切的完整屏幕截图。下面是 iOS 12 以下和 iOS 13 之后的输出图片。 Less than 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()

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

选项 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()

}

选项 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)
}
}

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

最佳答案

不要绘制图层,而是使用 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()

关于iOS 13 UITextView 将 UITextView 转换为图像无法正常工作。在 iOS 12 以下运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com