UITextView 滞后滚动并添加图像



我有一个UITextView,我在其中使用ImagePickerController添加图像。

如果其中只有文本,它是平滑的,但是在添加 3 或 4 张图像后,它在其内容中的滚动时会变得滞后。

我在添加图像时将其压缩到最低质量,但我一定做错了什么,因为它在第三张图像之后仍然滞后。

代码如下:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let _image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
self.dismiss(animated: true, completion: nil)
// Compress the retrieved image
let compressedImage = UIImage(data: _image.lowestQualityJPEGNSData!)
//create and NSTextAttachment and add your image to it.
let attachment = NSTextAttachment()
let oldWidth = compressedImage?.size.width // scales it within the UITextView
let scaleFactor = oldWidth! / (bodyEditText.frame.size.width - 10); // adjusts the desired padding
attachment.image = UIImage(cgImage: (compressedImage?.cgImage!)!, scale: scaleFactor, orientation: .up)
//put your NSTextAttachment into and attributedString
let attString = NSAttributedString(attachment: attachment)
//add this attributed string to the current position.
bodyEditText.textStorage.insert(attString, at: bodyEditText.selectedRange.location)
}
extension UIImage {
var uncompressedPNGData: Data?      { return UIImagePNGRepresentation(self)        }
var highestQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 1.0)  }
var highQualityJPEGNSData: Data?    { return UIImageJPEGRepresentation(self, 0.75) }
var mediumQualityJPEGNSData: Data?  { return UIImageJPEGRepresentation(self, 0.5)  }
var lowQualityJPEGNSData: Data?     { return UIImageJPEGRepresentation(self, 0.25) }
var lowestQualityJPEGNSData:Data?   { return UIImageJPEGRepresentation(self, 0.0)  }
}

可能导致此问题的原因是什么以及任何提示如何超越此问题?

感谢您的帮助

编辑

多亏了Duncan C,我设法消除了整个延迟并大大提高了渲染图像的性能。

这样,我就用以下内容替换了我的imagePickerController

let size = __CGSizeApplyAffineTransform(_image.size, CGAffineTransform.init(scaleX: 0.3, y: 0.3))
let hasAlpha = false
let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
_image.draw(in: CGRect(origin: CGPoint.zero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let att = NSTextAttachment()
att.image = scaledImage
//put your NSTextAttachment into and attributedString
let attString = NSAttributedString(attachment: att)
//add this attributed string to the current position.
bodyEditText.textStorage.insert(attString, at: bodyEditText.selectedRange.location)

您误用了比例因子。这旨在处理正常、视网膜和@3x视网膜图像的 1x、2x 和 3x 缩放。

按照您这样做的方式,系统每次想要绘制时都必须将全尺寸图像渲染到边界矩形中。如果图像比您显示它们的大小大得多,则会浪费大量内存和处理时间。

首先尝试简单地将图像视图的contentMode设置为.scaleAspectFit,并在图像视图中安装原始图像,而无需使用比例因子。看看效果如何。

如果这不能提供可接受的性能,则应将起始图像渲染到屏幕外图形上下文中,即图像的目标大小,并将重新渲染的图像安装到图像视图中。您可以使用UIGraphicsBeginImageContextWithOptions()或各种其他技术来调整图像大小以进行显示。

有关图像缩放和调整大小的信息,请查看此链接:

http://nshipster.com/image-resizing/

这样做不是最佳做法。UITextViews适用于文本而不是所有视图。

您需要为大量图像制作一个由 1、文本视图 2、UICollectionView 组成的自定义 UIView。

通过这种方式,您也可以在整个项目中的多个位置重用此自定义视图

最新更新