【问题标题】:iOS 7.0 UITextView gettings terribly slow after adding images to itiOS 7.0 UITextView 在添加图像后变得非常慢
【发布时间】:2014-04-12 06:29:28
【问题描述】:

我正在尝试在 ios7 及更高版本中使用 UITextView 制作文本编辑器,但我遇到了一些可怕的错误。我经历了许多与 textview 滚动相关的 Stack Overflow 问题。但是我无法找到的主要问题是在添加 NSTextAttachment(Custom) 后文本的渲染速度很慢。我正在使用这篇文章中描述的方法:

http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/

但是在添加图像后,文本的输入变得非常缓慢。代码与帖子中描述的几乎相同,所以我没有在这里粘贴。原因可以在以下问题中说: ios - iOS 7 UITextView is slow after typing lots of text

"drawGlyphsForGlyphRange 运行 N*2 次,其中 N 为次数 你的台词是自动换行的。”

但我不确定。有什么建议可以解决这种极其缓慢的文本渲染问题吗?

【问题讨论】:

    标签: ios uitextview


    【解决方案1】:

    我已经通过使用以下代码添加时缩放图像来解决滞后问题

    -(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 子类替换它们效率不高,并且会大大减慢渲染速度。

    【讨论】:

    • 这种方法是用缩放的图像替换原始粘贴的图像,因此无论何时您尝试在另一台设备上使用此属性字符串,您都可能会丢失原始图像质量。根据您的要求,您可能仍然可以在此处使用自定义附件路径(而不是在 willProcessEditing 中),但保留原始图像以及为设备缩放的图像。有点像缩略图。这里的缺点是增加了存储空间,但对于 iPhone 来说,图像的缩放比例很小,所以不太可能成为一个大问题——也许对于视网膜来说不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多