在我的应用程序中,我有一个UITableView的帖子。在那些UITableView单元格中,有UITextViews,其中包含解析的HTML到NSAttributedString。
我在模型中填充tableview之前调整图像大小
let mutableText = NSMutableAttributedString(attributedString: text)
mutableText.enumerateAttribute(NSAttributedStringKey.attachment, in: NSMakeRange(0, mutableText.length), options: .init(rawValue: 0), using: { (value, range, stop) in
if let attachement = value as? NSTextAttachment {
let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)!
let screenSize: CGRect = UIScreen.main.bounds
if image.size.width > screenSize.width {
attachement.bounds = CGRect(x: 0, y: 0, width: image.size.width * ((screenSize.width-75)/image.size.width), height: image.size.height * ((screenSize.width-75)/image.size.width))
}
}
})
但是,当我在TableView上滚动时,性能非常糟糕。当我到达图像时,它会停止滚动半秒钟。没有图像,滚动非常流畅。