iOS 7 UITextView: 重新打开应用后,NSTextAttachment 的大小变为2倍

7
我正在使用ios7中的Text Kit构建笔记编辑器。之前,我遇到了自定义大小的NSTextAttachment渲染问题,导致渲染速度非常慢。我通过缩放图像然后将它们添加到textview中解决了这个问题。你可以在iOS 7.0 UITextView gettings terribly slow after adding images to it找到我的答案。在缩放图像后,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。当运行新的应用程序会话时,我看到附件大小加倍。如果我尝试检查附件的大小并调整它,则滞后开始再次出现。我已经卡在这个问题上有一段时间了。任何建议将不胜感激。 enter image description here enter image description here

你是如何存储图片的?在缩放时,是否可能会修改附件中的原始图像,并在下次缩放时再次修改?当你第三次运行应用时会发生什么?应用程序如何知道何时不对图像进行缩放?也许可以在图像附件中设置一些附加属性,以便应用程序知道不对图像进行缩放。 - Duncan Groenewald
我正在将完整的NSAttributedString存储在核心数据中。 - nick28
  1. 是的,那可能是原因。无论是第三次运行还是之后,应用程序的行为都是相同的。
  2. 从我阅读其他问题中得到的信息是,默认的NSTextAttachment将图像的大小作为其边界。这就是帮助我制定解决方法并解决它的原因。
- nick28
如果我用子类化的对象替换NSTextattachment的实例,我会观察到一些延迟。你在iPhone上有没有观察到这样的情况,还是只有我遇到了? - nick28
下载 iWalletFree 并进行测试,看看是否出现相同的延迟。 - Duncan Groenewald
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.

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

我认为你可以尝试这个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接