【问题标题】:iOS CGContext Edge isn't antialiasediOS CGContext Edge 没有抗锯齿
【发布时间】:2016-02-04 23:29:37
【问题描述】:

我正在尝试绘制一个 15 度角的矩形,将两种颜色从中心分开,但它们之间的线是像素化的。我尝试了不同的混合选项,但似乎无法弄清楚如何使这个看起来干净。

This is what the header looks like now

有人知道我怎样才能让它们之间的界限看起来干净吗?

if (!_backgroundImageView.image) {
  CGFloat width = CGRectGetWidth(self.bounds);
  CGFloat height = CGRectGetHeight(self.bounds);
  CGFloat tWidth = 0.26794919243f/*tan(15 degrees)*/ * height;

  UIGraphicsBeginImageContext(CGSizeMake(width, height));
  CGContextRef context = UIGraphicsGetCurrentContext();

  if (_color1) {
    CGContextSetFillColorWithColor(context, _color1.CGColor);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 1.0f + RoundPixelValue((width + tWidth) / 2.0f), 0);
    CGContextAddLineToPoint(context, 1.0f + RoundPixelValue((width - tWidth) / 2.0f), height);
    CGContextAddLineToPoint(context, 0, height);
    CGContextFillPath(context);
  }

  if (_color2) {
    CGContextSetFillColorWithColor(context, _color2.CGColor);
    CGContextMoveToPoint(context, width, 0);
    CGContextAddLineToPoint(context, RoundPixelValue((width + tWidth) / 2.0f) - 1.0f, 0);
    CGContextAddLineToPoint(context, RoundPixelValue((width - tWidth) / 2.0f) - 1.0f, height);
    CGContextAddLineToPoint(context, width, height);
    CGContextFillPath(context);
  }

  _backgroundImageView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

【问题讨论】:

    标签: ios objective-c cgcontext uigraphicscontext


    【解决方案1】:

    这里的主要问题是您没有以正确的比例创建上下文。

    不要使用UIGraphicsBeginImageContext,而是像这样使用UIGraphicsBeginImageContextWithOptions

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0);
    

    将比例指定为 0 会将其设置为设备屏幕的比例因子。

    这应该消除了大部分的锯齿伪影。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多