2017-04-15 118 views
2

我有一个UITextView我添加图像,与ImagePickerController。UITextView滞后滚动添加图像

如果其中只有文本,它是平滑的,但添加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) } 
} 

什么可能会导致此问题的任何提示,我怎么能立交桥呢?

感谢您的帮助

编辑

感谢邓肯C I已经设法删除整个滞后和提高性能的渲染图像了很多。

通过,我已经取代了我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) 

回答

1

你滥用比例因子。这是为了处理正常,视网膜和@ 3x视网膜图像的1倍,2倍和3倍缩放。

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

首先尝试简单地将图像视图的contentMode设置为.scaleAspectFit,然后将原始图像安装在图像视图中,而无需考虑比例因子。看看如何执行。

如果不给,你应该使你的启动映像导入到你的图像的目标尺寸屏幕外的图形上下文可接受的性能,并重新绘制图像安装到您的图像视图。您可以使用UIGraphicsBeginImageContextWithOptions()或各种其他技术来调整图像的大小以供显示。

看看这个链接的信息,图像缩放和调整:

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

+0

有一两件事,我没有检索到的图像设置为一个ImageView的,我将其添加为附件的NSAttributedString;我该如何aspectFit呢? –

+0

感谢您的回答我相信我会解决我的问题与您的解释 –

+0

哦,我没有看到这一部分。你有代码'attachment.image = ...'缩放你的图像后,将其分配给attachment.image。 –

1

它不是最好的做法,以做到这一点。 UITextViews适用于文本而非所有视图。

您需要制作一个由1,textview 2,UICollectionView组成的自定义UIView,用于许多图像。

这样您可以重新过整个项目在许多地方这个自定义视图

+0

嗯......这实际上是一个不错的提示,谢谢 –

+0

如果你将这个答案掩盖为有用的话,我将非常感激:) – chetan