In my app I have a UITableView of posts. In those UITableView cells there are UITextViews with parsed HTML to an NSAttributedString.
I resize the images before the tableview is populated in a model
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))
}
}
})
However, when I scroll on the TableView the performance is horrible. When I reach an image it stops scrolling for half a second. Without images, scrolling is quite smooth.