7

我正在使用 ios7 中的 Text Kit 构建一个笔记编辑器。早些时候,我在渲染自定义大小的 NSTextAttachment 时遇到了麻烦,因为它在很大程度上减慢了渲染速度。我通过缩放图像然后将它们添加到 textview 解决了这个问题。你可以在 iOS 7.0 UITextView 中找到我的答案,之后变得非常慢向其中添加图像 缩放图像后,textview 渲染运行良好,没有任何延迟。textview 的属性文本存储在核心数据中。在应用程序的运行会话期间,textview 正确显示图像。即使在核心数据中保存属性文本并检索它之后再次显示在 textview 上,图像看起来很好。但是在杀死应用程序并再次运行应用程序后。图像放大到 2 倍大小。在缩放图像时,我使用以下函数并使用 [[UIScreen bounds] scale] 来维护图像质量。

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

     UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

如果我将图像缩放到 1.0,图像不会扩展,但图像质量非常差。

我认为问题出在哪里? 问题在于布局管理器。

我尝试过 的我尝试将 NSLayoutManager 子类化并覆盖 - (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin 我看到的是在运行应用程序的新会话时附件大小加倍。如果我尝试检查附件的大小并调整它的大小。滞后又开始了。我很早就被这个问题困扰了。任何建议都将不胜感激。在此处输入图像描述 在此处输入图像描述

4

2 回答 2

4

会不会是视网膜显示的原因?如果是视网膜,您可能需要在存储前将尺寸缩小 50%。试试这个怎么样: -

 //Original Size that you want to store
CGSize imageSize = CGSizeMake(320.0f, 320.0f);

//Make the image 50% of the size for retina
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&([UIScreen mainScreen].scale == 2.0)) {
    // Retina display
    imageSize = CGSizeMake(160.0f, 160.0f);
}

UIImage * storeImage = [self imageWithImage:self.image scaledToSize:imageSize]
//TODO: Store this image locally or whatever you want to do.
于 2014-05-05T06:35:32.480 回答
2
@interface MMTextAttachment : NSTextAttachment
{
}
@end
@implementation MMTextAttachment
//I want my emoticon has the same size with line's height
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0)
{
return CGRectMake( 0 , 0 , lineFrag.size.height , lineFrag.size.height );
}
@end

我想你可以试试这个。

于 2015-03-20T08:45:09.753 回答