2016-02-04 3 views
0

J'essaie de dessiner un rectangle avec un angle de 15 degrés séparant deux couleurs au centre, mais la ligne entre elles est pixellisée. J'ai essayé différentes options de mélange et je n'arrive pas à comprendre comment le faire paraître propre.iOS CGContext Edge n'est pas anti-crénelé

This is what the header looks like now

Est-ce que quelqu'un sait comment je peux faire la ligne entre eux l'air propre?

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

Répondre

0

Le problème principal ici est que vous ne créez pas le contexte avec l'échelle correcte.

Au lieu d'utiliser UIGraphicsBeginImageContext utiliser UIGraphicsBeginImageContextWithOptions comme ceci:

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

Spécification une échelle de 0 ensembles au facteur d'échelle de l'écran de l'appareil.

Ceci devrait éliminer la plupart des artefacts d'aliasing.