【问题标题】:Anti-aliasing UIimage looks blurred or jagged抗锯齿 UIimage 看起来模糊或锯齿状
【发布时间】:2013-01-02 12:21:21
【问题描述】:

我想在代表手表编号的圆圈中绘制 12 个图像,我已阅读 stackoverflow 上有关具有透明边框的图像的所有主题,但它不适用于我的情况

-(UIImage *)addImageNumber_:(UIImage *)img {

    int w = img.size.width;
    int h = img.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
    CGContextSetShouldAntialias(context,YES);
    CGContextSetAllowsAntialiasing( context ,YES );
    CGAffineTransform transform;

    for (int x=0; x<=11; x++) {

        UIImage *timg1 = [UIImage imageNamed:@"2.png"];

        CGRect imageRect = CGRectMake(0, 0, timg1.size.width+2, timg1.size.height+2);
        UIGraphicsBeginImageContext(imageRect.size);
        [timg1 drawInRect:CGRectMake(1,1,timg1.size.width,timg1.size.height)];
        timg1 = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        transform = CGAffineTransformIdentity;

        CGContextDrawImage(context, CGRectMake((w-26)/2, 0, 26, 30), timg1.CGImage);

        transform = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(-w/2, -w/2));
        transform = CGAffineTransformConcat(transform, CGAffineTransformMakeRotation(radians(30)));
        transform = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(w/2, w/2));

        CGContextConcatCTM(context, transform);
    }

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}
UIViewEdgeAntialiasing = YES;
UIImage *img = [UIImage imageNamed:@"test.png"];
UIImage *img2 = [self addImageNumber_:img ];
R1.image = img2;
[self.view addSubview:R1];

测试img是手表的背景,2.png是透明png,边框透明

12 点和 6 点的数字看起来不错,因为它们没有旋转,其余的都是锯齿状的

【问题讨论】:

  • 抗锯齿不应该是 UIImageViews 的问题,因为它们默认情况下是抗锯齿的,而 UIView 通常显示为锯齿状。我认为这可能是另一个问题。您是否正在大幅缩小您正在使用的图像?
  • 所有图片都没有被缩小,它们都是原始大小
  • 您获得的质量是技术上可行的。创建原始图像时,会使用最佳抗锯齿。当你然后旋转它时,它不再是最佳的。它只是随着每次转换而失去质量。最好使用文字绘图功能来创建时钟。
  • 这是最初的方式,但它更加模糊,所以我决定采用图像方式
  • 图像总是比实际文本更模糊。 @Codo 是对的。如果您只使用图片是因为您第一次尝试文本没有成功,请再试一次。

标签: iphone ios image uiimageview uiimage


【解决方案1】:

永远不要说UIGraphicsBeginImageContext(imageRect.size)。说UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0)。这样,在双分辨率屏幕上,您将获得双分辨率图形上下文。

您甚至可以尝试将分辨率值设为 4 以进一步提高分辨率。

当然,您从预先绘制的“2”图像开始这一事实可能会限制您的分辨率;你不能用母猪的耳朵做一个丝绸钱包。从头开始将“2”绘制为字符串可能会好得多。

【讨论】:

  • 我用字符串而不是图像附加了一个图像,但我的抗锯齿仍然不起作用
  • 但是你没有按照我所说的以更高分辨率绘制,是吗?
  • 所以你没有回应我说的话。在你拒绝之前尝试我所说的尝试。
【解决方案2】:

试试

CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

【讨论】:

    【解决方案3】:

    斯威夫特 3:

    context!.interpolationQuality = .high
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 2011-07-05
      相关资源
      最近更新 更多