使用问题CIAttributedTextImageGenerator在iOS中绘制文本

时间:2020-04-21 09:00:56

标签: ios avfoundation core-image cifilter

示例代码:

    NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"Hello world" attributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:100],
        NSForegroundColorAttributeName:[UIColor redColor]
    }];
    CIFilter *textGenerator = [CIFilter filterWithName: @"CIAttributedTextImageGenerator"
                                           keysAndValues: @"inputText", text,nil];
    CIImage *image= [textGenerator valueForKey: @"outputImage"];

当我更改同一文本的文本颜色时,例如[UIColor blueColor],然后再次过滤。我得到的旧文本纹理为redColor。

我发现除非将文本内容更改为其他内容(获取正确的纹理)或缩放纹理(具有新旧纹理的显示开关错误)。

这是一个diff错误或CIAttributedTextImageGenerator功能...?

如何获得合适的质地...?

只能将其用作基本纹理,并且可以使用其他CIFilter渲染颜色吗?

还是有其他方法可以快速渲染文本?

谢谢!!!

我编写了一个简单的演示代码,如下所示:

NSAttributedString *firstText = [[NSAttributedString alloc] initWithString:@"Hello world"
                                                           attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:100],
                                                                        NSForegroundColorAttributeName:[UIColor redColor]}];
CIFilter *textGenerator1 = [CIFilter filterWithName: @"CIAttributedTextImageGenerator"
                                     keysAndValues: @"inputText", firstText,nil];
CIImage *texture1= [textGenerator1 valueForKey: @"outputImage"];
self.firstImageView.image = [UIImage imageWithCIImage:texture1];

NSAttributedString *secondText = [[NSAttributedString alloc] initWithString:@"Hello world"
                                                           attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:100],
                                                                        NSForegroundColorAttributeName:[UIColor blueColor]}];
CIFilter *textGenerator2 = [CIFilter filterWithName: @"CIAttributedTextImageGenerator"
                                     keysAndValues: @"inputText", secondText,nil];
CIImage *texture2= [textGenerator2 valueForKey: @"outputImage"];
self.secondImageView.image = [UIImage imageWithCIImage:texture2];

显示结果: [https://i.stack.imgur.com/NABBu.png][1]

0 个答案:

没有答案