【问题标题】:UITextView lags scrolling with added imagesUITextView 延迟滚动添加的图像
【发布时间】:2017-09-11 17:30:38
【问题描述】:

我有一个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)

【问题讨论】:

    标签: ios swift3 uiscrollview uitextview nsattributedstring


    【解决方案1】:

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

    按照您的操作方式,系统必须在每次要绘制一个时将全尺寸图像渲染到您的边界矩形中。如果图像比您显示的尺寸大很多,那么您会浪费大量内存和处理时间。

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

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

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

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

    【讨论】:

    • 一件事,我没有将检索到的图像设置为 imageView,而是将其作为附件添加到 NSAttributedString;我怎样才能适应它?
    • 谢谢你的回答,我相信我会用你的解释解决我的问题
    • 哦,我没看到那部分。您有代码 attachment.image = ... 缩放图像后,将其分配给 attachment.image。
    【解决方案2】:

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

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

    通过这种方式,您也可以在整个项目的许多地方重复使用此自定义视图

    【讨论】:

    • 如果您将此答案视为有用,我将不胜感激:)
    猜你喜欢
    • 2016-07-10
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    相关资源
    最近更新 更多