2016-02-04 61 views
0

我想繪製一個矩形,有一個15度角沿着中心分裂兩種顏色,但他們之間的線是像素化的。我嘗試了不同的混合選項,似乎無法弄清楚如何讓這看起來乾淨。iOS的CGContext邊緣沒有antialiased

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(); 
} 

回答

0

這裏的主要問題是您沒有使用正確的比例創建上下文。

代替使用UIGraphicsBeginImageContext使用UIGraphicsBeginImageContextWithOptions像這樣的:

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

指定爲0集的規模它到裝置屏幕的比例因子。

這應該消除大部分的混疊僞像。