ios - iOS CGContext Edge没有抗锯齿

标签 ios objective-c cgcontext uigraphicscontext

我正在尝试绘制一个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();
}

最佳答案

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

不要像这样使用UIGraphicsBeginImageContext来使用UIGraphicsBeginImageContextWithOptions:

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

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

这应该消除了大多数混淆现象。

关于ios - iOS CGContext Edge没有抗锯齿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35213703/

相关文章:

ios - 使用 UITableView 创建照片提要

ios - 在 Swift 中选择 UIPickerView 时如何隐藏键盘

iphone - 使用 HTTP NSURL 创建 AVAsset

iphone - 如何将 NSUndoManager 与 UIImageView 或 CGContext 一起使用

ios - 在 UITableViewCell 中显示突出显示的图像

objective-c - 互动书 - 从哪里开始

objective-c - 如何将图像添加到 Core Plot CPT BarPlot 图层?

iphone - 如何使 View 具有半透明边框并显示下面的 View

ios - 在 swift 3 中将 CMSampleBuffer 旋转任意角度并附加到 AVAssetWriterInput

objective-c - iPad绘图:将图像绘图到CGContext中