添加图像后,iOS 7.0 UITextView 变得非常慢 [英] iOS 7.0 UITextView gettings terribly slow after adding images to it
问题描述
我正在尝试在 ios7 及更高版本中使用 UITextView 制作文本编辑器,但我遇到了一些可怕的错误.我已经解决了许多与滚动 textview 相关的 Stack Overflow 问题.但我无法找到的主要问题是在向文本添加 NSTextAttachment(Custom) 后缓慢呈现文本.我正在使用这篇文章中描述的方法:
I am trying to make a text editor using UITextView in ios7 and above, but I am facing some terrible bugs. I have gone through many Stack Overflow questions related to scrolling of textview. But the main issue about which I am not able to find is slow rendering of text after adding NSTextAttachment(Custom) to it. I am using the approach described on this post:
但是添加图像后,文本输入变得非常缓慢.代码与帖子中描述的几乎相同,所以我没有粘贴在这里.原因如以下问题所述:ios - iOS 7 UITextView 输入大量后很慢文本
But after adding the images the typing of text becomes terribly slow. The code is almost the same as described in the post so I have not pasted it here. The reason can be as said in the following question: ios - iOS 7 UITextView is slow after typing lots of text
"drawGlyphsForGlyphRange 运行 N*2 次,其中 N 是次数你的台词是自动换行的."
"drawGlyphsForGlyphRange runs N*2 times, where N is the number of times your lines word-wrapped."
但我不确定.关于解决这种极其缓慢的文本渲染的方向有什么建议吗?
But I am not sure. Any advice in a direction to solve this terribly slow rendering of text?
推荐答案
我使用以下代码添加时通过缩放图像解决了延迟
I have solved the lag by scaling the images when adding using the following code
-(void)insertImage:(UIImage *)image
{
NSTextAttachment* attachment = [[NSTextAttachment alloc] initWithData:UIImageJPEGRepresentation(image, 0.0) ofType:@"image/jpeg"];
float scalingFactor = 1.0;
CGSize imageSize = [image size];
float width = self.frame.size.width;
if (width < imageSize.width)
scalingFactor = (width)*scalingFactor / imageSize.width;
CGRect rect = CGRectMake(0, 0, imageSize.width*scalingFactor, imageSize.height *scalingFactor);
attachment.image = [self imageWithImage:image scaledToSize:rect.size];
attachment.bounds = [self scaleImageSizeToWidth:self.frame.size.width withImage:image];
NSRange range = [self selectedRange];
NSAttributedString* attachmentchar =
[NSAttributedString attributedStringWithAttachment:attachment];
[[self textStorage] insertAttributedString:attachmentchar atIndex:range.location];
}
我注意到使用 - (void)textStorage:(NSTextStorage *)textStorage willProcessEditing:(NSTextStorageEditActions)editedMask range:(NSRange)editedRange changeInLength:(NSInteger)delta 并枚举附件的文本存储并用自定义 nstextattachment 子类替换它们效率低下,并且会大大减慢渲染速度.
I noticed using the - (void)textStorage:(NSTextStorage *)textStorage willProcessEditing:(NSTextStorageEditActions)editedMask range:(NSRange)editedRange changeInLength:(NSInteger)delta and enumerating the text storage for attachments and replacing them with custom nstextattachment subclass is not productive and is slowing down the rendering a lot.
这篇关于添加图像后,iOS 7.0 UITextView 变得非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!