0

次のメソッドは、塗りつぶされた三角形の画像を作成する必要がありますが、アウトラインのみを作成します。FILLしないのはなぜですか?これで大騒ぎします。これに苦しんでいる次の貧しい魂がハードルをクリアし、彼らの命を1時間救うことができるように、それが答えられることを願っています.

ここに私が書いた方法があります:

+ (UIImage *)triangleWithSize:(CGSize)imageSize
{
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();    
    CGContextSetShouldAntialias(context, YES);

    // set parameters
    CGContextSetLineWidth(context, 1);
    CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);

    // draw triangle
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, imageSize.width, 0);
    CGContextAddLineToPoint(context, imageSize.width, imageSize.height);
    CGContextMoveToPoint(context, imageSize.width, imageSize.height);
    CGContextAddLineToPoint(context, 0, imageSize.height / 2);
    CGContextMoveToPoint(context, 0, imageSize.height / 2); 
    CGContextAddLineToPoint(context, imageSize.width, 0);
//    CGContextFillPath(context);
  //  CGContextClosePath(context);

    // stroke and fill?
    CGContextDrawPath(context, kCGPathFillStroke);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIGraphicsPopContext();

    return image;    
}

どんな助けでも大歓迎です。

4

1 に答える 1

2

次の投稿から答えを見つけました: CGContext線画:CGContextFillPathが機能していませんか?

最後の回答の1つは、CGMutablePathRefを使用した例を示しています。三角形の場合、次のコードを使用しました。

CGMutablePathRef pathRef = CGPathCreateMutable();

CGPathMoveToPoint(pathRef, NULL, imageSize.width, 0);
CGPathAddLineToPoint(pathRef, NULL, imageSize.width, imageSize.height);
CGPathAddLineToPoint(pathRef, NULL, 0, imageSize.height / 2);
CGPathAddLineToPoint(pathRef, NULL, imageSize.width, 0);

CGPathCloseSubpath(pathRef);

CGContextAddPath(context, pathRef);
CGContextFillPath(context);

CGContextAddPath(context, pathRef);
CGContextStrokePath(context);

CGPathRelease(pathRef);    

ただし、上記で使用した元の方法が機能しない理由はわかりません。次にそれを理解します。

于 2012-06-23T23:32:51.443 に答える