使用 CGContextDrawImage renderInContext 时 UILabel 文本像素化
我在 iPhone 上使用 CoreText 来获取 UIScrollView 中的斜体文本。 iPhone 上的 CoreText 不支持图像。因此,我告诉 CoreText 为图像提供一些空间,然后使用 CGContextDrawImage 在 300 x 120(比例为 2.5)的矩形中绘制图像 BlendedImage。创建的图像(在下面的代码中为 blendedImage)具有相同的尺寸比例,因为 175/70 = 2.5。由于我创建的图像 blendedImage 也包含我想要自动换行的文本,因此我使用 renderInContext 为图像中的 UILabel 创建图像。问题在于 UILabels 中的文本模糊且像素化。有谁知道我该如何解决这个问题?
这是代码:
CGRect dccFrame = CGRectIntegral(CGRectMake(0, 60, 70, 72)); // 20, 60, 100, 72
UILabel *dccLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(dccFrame)];
dccLabel.backgroundColor = [UIColor clearColor];
dccLabel.numberOfLines = 0;
dccLabel.lineBreakMode = UILineBreakModeWordWrap;
NSString *titledccString = self.dccString;
dccLabel.textColor = [UIColor blackColor];
dccLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:7];
[dccLabel setText:titledccString];
[dccLabel sizeToFit];
CGSize newSize = CGSizeMake(176, 70);
UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(gc, 1, 1, 1, 1);
CGContextSetShouldSmoothFonts(gc, YES);
CGContextSetAllowsAntialiasing(gc, YES);
CGContextSetShouldAntialias(gc, YES);
UIGraphicsPushContext(gc);
CGAffineTransform trf1 = CGAffineTransformMakeTranslation(0, 0);
CGContextSaveGState(gc);
CGContextConcatCTM(gc, trf1);
[[UIImage imageNamed: imageString] drawInRect:CGRectIntegral(CGRectMake(70, 0, 46, 60))];
CGContextRestoreGState(gc);
UIGraphicsPopContext();
CGAffineTransform trf3 = CGAffineTransformMakeTranslation(0, 30);
CGContextSaveGState(gc);
CGContextConcatCTM(gc, trf3);
[dccLabel.layer renderInContext:gc];
CGContextRestoreGState(gc);
UIImage *blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[dccLabel release];
我读过,如果位置不是整数,可能会导致像素化。但我认为这不是问题所在。我还在核心动画模式下运行了 Instruments 并启用了“颜色未对齐图像”,但我创建的图像没有显示为彩色,尽管其他图像显示为彩色。
有谁知道为什么 UILabel dccLabel 中的文本模糊且像素化以及如何修复它?
I am using CoreText on the iPhone to get italic text in a UIScrollView. CoreText on the iPhone doesn't support images. So I tell CoreText to provide some space for an image and then use CGContextDrawImage to draw an image, blendedImage, in a rectangle of 300 by 120 which is a 2.5 ratio. The image that is created, which is blendedImage in the code below, has the same size ration, since 175/70 = 2.5. Since the image, blendedImage, I am creating has text that I want to word wrap as well, I create the image using renderInContext for the UILabels that are in the image. The problem is that the text in the UILabels is blurred and pixelated. Does anyone know how I can fix this?
Here is the code:
CGRect dccFrame = CGRectIntegral(CGRectMake(0, 60, 70, 72)); // 20, 60, 100, 72
UILabel *dccLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(dccFrame)];
dccLabel.backgroundColor = [UIColor clearColor];
dccLabel.numberOfLines = 0;
dccLabel.lineBreakMode = UILineBreakModeWordWrap;
NSString *titledccString = self.dccString;
dccLabel.textColor = [UIColor blackColor];
dccLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:7];
[dccLabel setText:titledccString];
[dccLabel sizeToFit];
CGSize newSize = CGSizeMake(176, 70);
UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(gc, 1, 1, 1, 1);
CGContextSetShouldSmoothFonts(gc, YES);
CGContextSetAllowsAntialiasing(gc, YES);
CGContextSetShouldAntialias(gc, YES);
UIGraphicsPushContext(gc);
CGAffineTransform trf1 = CGAffineTransformMakeTranslation(0, 0);
CGContextSaveGState(gc);
CGContextConcatCTM(gc, trf1);
[[UIImage imageNamed: imageString] drawInRect:CGRectIntegral(CGRectMake(70, 0, 46, 60))];
CGContextRestoreGState(gc);
UIGraphicsPopContext();
CGAffineTransform trf3 = CGAffineTransformMakeTranslation(0, 30);
CGContextSaveGState(gc);
CGContextConcatCTM(gc, trf3);
[dccLabel.layer renderInContext:gc];
CGContextRestoreGState(gc);
UIImage *blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[dccLabel release];
I have read that if the positions are not integer that can cause pixelation. But I don't think that is the problem here. I've also run Instruments in Core Animation mode and enabled "Color Misaligned Images" but the image I created doesn't show up as colored, although other images do.
Does anyone know why the text in the UILabel dccLabel is blurred and pixelated and how I can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了,花了我一段时间!答案很简单。只需使 newSize 与绘制图像的矩形大小相同即可。
使图像大小与绘制图像的矩形大小相同是有意义的,对吗?
Figured it out, took me awhile ! And the answer is quite simple. Just had to make newSize the same size as the rectangle the image was being drawn into.
Kind of makes sense to make the image size the same size as the rectangle that the image is being drawn into, right?