简体   繁体   English

NSAttributedString上的模糊图像(NSTextAttachment)

[英]Blurry image (NSTextAttachment) on NSAttributedString

I'm using NSAttributedString to include images in a string. 我正在使用NSAttributedString将图像包含在字符串中。 However, the images are sometimes blurry like if were draw on a non-integer frame. 但是,图像有时会模糊,就像在非整数帧上绘制一样。

I tried to make sure the bound of each NSTextAttachment is of integer size, but that doesn't seem to help. 我试图确保每个NSTextAttachment的边界是整数大小,但这似乎没有帮助。 Any tips on how to make sure it's not blurry? 关于如何确保它不模糊的任何提示?

See attached screenshot, the first bus is not blurry but the second is. 参见随附的截图,第一辆公交车并不模糊,但第二辆公交车是模糊的。

在此输入图像描述

I fixed this by adding a category on NSAttributedString. 我通过在NSAttributedString上添加一个类别来解决这个问题。

Basically, you need to get the NSTextAttachment's frame and add the missing fractional part of its X coordinate to have it round up nicely. 基本上,您需要获取NSTextAttachment的框架并添加其X坐标的缺失小数部分以使其很好地向上舍入。

- (void)applyBlurrinessFixToAttachments {
    [self enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, self.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (![value isKindOfClass:[NSTextAttachment class]]) {
            return;
        }
        NSTextAttachment *attachment = (NSTextAttachment*)value;
        CGRect bounds = attachment.bounds;
        CGRect attributedStringRect = [self boundingRectForCharacterRange:range];

        double integral;
        double fractional = modf(attributedStringRect.origin.x, &integral);
        if (fractional > 0) {
            double roundedXOrigin = 1.0 - fractional;

            // If X coordinate starts at 0.7, add 0.3 to it
            bounds.origin.x += roundedXOrigin;
            attachment.bounds = bounds;
        }
    }];
}

- (CGRect)boundingRectForCharacterRange:(NSRange)range {
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
textContainer.lineFragmentPadding = 0;
    [layoutManager addTextContainer:textContainer];

    NSRange glyphRange;
    [layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];

    return [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
}

you can use below code for image round 你可以使用下面的代码进行图像轮

UIGraphicsBeginImageContextWithOptions(CGSizeMake(14, 14), NO, [UIScreen mainScreen].scale); UIGraphicsBeginImageContextWithOptions(CGSizeMake(14,14),NO,[UIScreen mainScreen] .scale);

-(UIImage *)makeRoundedImage:(UIImage *) image radius: (float) radius {
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, 14, 14);
imageLayer.contents = (id) image.CGImage;

imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(14, 14), NO, [UIScreen mainScreen].scale);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return roundedImage;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调整图像大小或作为 NSAttributedString NSTextAttachment 完成(或设置其初始大小) - how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size) 如何将NSTextAttachment插入NSAttributedString? - How to insert NSTextAttachment into NSAttributedString? 获取嵌入在NSAttributedString中的图像(通过NSTextAttachment)被视为单个字符,以便它不会破坏线条? - Getting an image imbedded in an NSAttributedString (via NSTextAttachment) to be treated as a single character, so that it doesn't break lines? 可以存储(或恢复)包含 NSTextAttachment 的 NSAttributedString 吗? - Can NSAttributedString which contains NSTextAttachment be stored(or restored)? 如何检测NSAttributedString是否包含NSTextAttachment并将其删除? - How to detect if a NSAttributedString contains a NSTextAttachment and remove it? 使用NSTextAttachment为NSAttributedString设置截断尾部的垂直对齐 - Setting vertical align of truncated tails for NSAttributedString with NSTextAttachment 如何在NSAttributedString中删除和替换NSTextAttachment - How do you remove and replace an NSTextAttachment in an NSAttributedString NSTextAttachment 图像动画可能吗? - Is NSTextAttachment image animation possible? 为什么不显示NSTextAttachment图像? - Why is NSTextAttachment image not displayed? 更改NSTextAttachment图像的大小? - Change size of NSTextAttachment Image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM