【问题标题】:Drawing anti-aliased line绘制抗锯齿线
【发布时间】:2014-10-24 14:56:27
【问题描述】:

我正在屏幕上画一条线,其中包括对角线,而对角线又是这样绘制的:

注意它看起来一点也不平滑。我已经阅读了几篇关于此的文章,似乎 this 看起来更接近我遇到的问题,但该解决方案对我不起作用。

这是我设置图层的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Instantiate the navigation line view
    CGRect navLineFrame = CGRectMake(0.0f, 120.0f, self.view.frame.size.width, 15.0f);
    self.navigationLineView = [[HyNavigationLineView alloc] initWithFrame:navLineFrame];

    // Make it's background transparent
    self.navigationLineView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
    self.navigationLineView.opaque = NO;

    HyNavigationLineLayer * layer = [[HyNavigationLineLayer alloc] init];
    layer.shouldRasterize = YES;
    layer.rasterizationScale = [UIScreen mainScreen].scale;
    layer.contentsScale = [[UIScreen mainScreen] scale];
    layer.needsDisplayOnBoundsChange = YES;

    [[self.navigationLineView layer] addSublayer:layer];
    [self.view addSubview:self.navigationLineView];
    [self.navigationLineView.layer setNeedsDisplay];
}

这是我在图层中的绘制方式:

- (void)drawInContext:(CGContextRef)ctx
{
    CGFloat bottomInset = 1.0f / [[UIScreen mainScreen] scale] * 2.0f;

    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);

    GLfloat padding = kHyNavigationLineViewPadding * 4.0f;
    GLfloat searchSpace = self.frame.size.width - padding - kHyNavigationLineViewPointerSize * 2.0f;
    GLfloat x = kHyNavigationLineViewPadding * 2.0f + searchSpace * self.offset;

    CGContextSetLineWidth(ctx, 2.0f);
    CGContextMoveToPoint(ctx, kHyNavigationLineViewPadding, 0.0f);
    CGContextAddLineToPoint(ctx, x, bottomInset);
    CGContextAddLineToPoint(ctx, x + kHyNavigationLineViewPointerSize, self.frame.size.height);
    CGContextAddLineToPoint(ctx, x + kHyNavigationLineViewPointerSize * 2.0f, bottomInset);
    CGContextAddLineToPoint(ctx, self.frame.size.width - kHyNavigationLineViewPadding, 0.0f);

    // Draw
    CGContextStrokePath(ctx);
}

关于如何解决这个问题的任何提示?

编辑:忘了提到我首先在图层中绘制的原因:我需要为使这条线动画的属性设置动画效果很好,所以在drawRect 中绘制不起作用。

【问题讨论】:

  • 你在哪里/何时告诉图层自己绘制?什么时候设置图层的框架?具体来说,我想知道是否有任何方法可以在图层较小的情况下绘制图层的内容,然后在布局期间按比例放大。一种检查方法是在图层设置期间设置layer.needsDisplayOnBoundsChange = YES;,看看是否能解决问题。
  • 请看我的编辑。 layer.needsDisplayOnBoundsChange 似乎没有帮助...
  • 谢谢。耻辱!那么你HyNavigationLineLayer 的框架呢?这是在哪里设置的?
  • 在调试器中暂停并运行以下命令:po [[UIApp keyWindow] recursiveDescription]。然后,在输出中找到您的HyNavigationLineLayer 的地址。在上面运行po。 (例如:如果输出包含<HyNavigationLineLayer: 0x79f5aee0>,则运行po 0x79f5aee0。)复制两个命令的所有输出并将其粘贴到您的问题中。
  • @stefandouganhyde,这就是我的所有设置......

标签: ios objective-c calayer antialiasing


【解决方案1】:

你可以使用 UIBezierPath 代替使用quartz来渲染代码:

UIBezierPath* path = [UIBezierPath bezierPath];

[[UIColor whiteColor] setStroke];

GLfloat padding = kHyNavigationLineViewPadding * 4.0f;
GLfloat searchSpace = self.frame.size.width - padding - kHyNavigationLineViewPointerSize * 2.0f;
GLfloat x = kHyNavigationLineViewPadding * 2.0f + searchSpace * self.offset;

path.lineWidth = 2.0;
[path moveToPoint:CGPointMake(kHyNavigationLineViewPadding, 0)];
[path addLineToPoint:CGPointMake(x, bottomInset)];
[path addLineToPoint:CGPointMake(x + kHyNavigationLineViewPointerSize, self.frame.size.height)];
[path addLineToPoint:CGPointMake(x + kHyNavigationLineViewPointerSize * 2.0f, bottomInset)];
[path addLineToPoint:CGPointMake(self.frame.size.width - kHyNavigationLineViewPadding, 0.0f)];

[path stroke];

【讨论】:

  • :CGContextRestoreGState:无效上下文 0x0。这是一个严重的错误。此应用程序或它使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体下降。此通知是出于礼貌:请解决此问题。这将成为即将到来的更新中的致命错误。
  • 我不应该在drawInContext:中画画吗?
  • 你应该在被 UIView 覆盖的-(void)drawRect:(CGRect)rect 中进行操作
  • 很抱歉我的回复晚了,我一直很忙。我无法在drawRect: 中绘图,因为我需要为该属性设置动画。这就是我首先使用图层的原因。
  • 尝试删除这些行layer.shouldRasterize = YES; layer.rasterizationScale = [UIScreen mainScreen].scale;
猜你喜欢
  • 2015-08-15
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多