2012-03-30 11 views
1

Ich versuche, eine Routine zu programmieren, die mir erlauben würde, ein UIImage zu erstellen, das ein Rechteck ist, das mit Streifen auf einem Hintergrund mit Farbverlauf gefüllt ist.Gestreifte Linien erscheinen aus dem Nichts

Mein Code ist unten und funktioniert gut für die meisten Fälle, die ich ausprobiert habe. Interessanterweise, und das ist der Haken, funktioniert es nicht, wenn ich 21 als Höhe und 5 als StripeWidth übergebe.

Sobald ich dies tun, erscheinen die Streifen wie sie sollen ... horizontal .. aber vertikal beginnen sie bei wie (y =) -40 und enden bei etwa (y =) 4 oder so. Um dieses besser zu sehen, jedes dieses Bild zeigt den Effekt in Frage:

Hat jemand eine Idee, warum das passiert, oder noch besser, was kann ich dagegen tun?

enter image description here

-(UIImage*) stripedTextureWithStartingColor:(UIColor*) startColor withEndingColor:(UIColor*) endColor withHeight:(NSUInteger) height withStripeWidth:(NSUInteger) stripeWidth withStripeColor:(UIColor*) stripedColor { 
    CGSize size = CGSizeMake(2 * stripeWidth, height); 
    UIGraphicsBeginImageContext(size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 
    @try { 
    CGContextSetAllowsAntialiasing(context, true); 
    CGContextSetShouldAntialias(context, true); 
    NSArray* colors = [NSArray arrayWithObjects:(id) startColor.CGColor, (id) endColor.CGColor, nil]; 
    CGFloat locations[2]; 
    locations[0] = 0.0; 
    locations[1] = 1.0; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); 
    CGColorSpaceRelease(colorSpace); 
    CGContextDrawLinearGradient(context, gradient, CGPointZero, CGPointMake(0, size.height - 1), 0); 
    CGGradientRelease(gradient); 
    CGContextFillPath(context); 

    int lineWidth = (int) stripeWidth; 
    CGContextSetLineWidth(context, lineWidth/2); 
    int lineCount = (float) size.height/(float) lineWidth; 
    lineCount -= 2; 

    for (int i=0; i<lineCount; i++) { 
     CGContextSetStrokeColorWithColor(context, stripedColor.CGColor); 
     float x1 = -size.height + i * 2 * lineWidth - lineWidth; 
     float y1 = size.height - 1 + lineWidth; 
     float x2 = -size.height + i * 2 * lineWidth + size.height - lineWidth; 
     float y2 = -lineWidth; 
     CGContextMoveToPoint(context, x1, y1); 
     CGContextAddLineToPoint(context, x2, y2); 
     CGContextStrokePath(context); 
    } 

    UIColor* lineTopColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9]; 
    UIColor* lineBottomColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.5]; 

    CGContextSetStrokeColorWithColor(context, lineTopColor.CGColor); 
    CGContextMoveToPoint(context, 0, 0); 
    CGContextAddLineToPoint(context, size.width + 1, 0); 
    CGContextStrokePath(context); 

    CGContextSetStrokeColorWithColor(context, lineBottomColor.CGColor); 
    CGContextMoveToPoint(context, 0, size.height - 1); 
    CGContextAddLineToPoint(context, size.width + 1, size.height - 1); 
    CGContextStrokePath(context); 

    } 
    @finally { 
    CGContextRestoreGState(context); 
    } 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

Antwort

1

gefunden, mein Algorithmus mit dem Start der Linien

+0

könnten Sie nach, was genau falsch war etwas falsch war? –

Verwandte Themen