【问题标题】:Draw a colored triangle in ios在ios中绘制一个彩色三角形
【发布时间】:2013-11-08 14:05:20
【问题描述】:

我想用给定的线条颜色、线条粗细和填充颜色绘制一个三角形。 但是有些我无法正确填充三角形。每次三角形周围的整个区域也被填充。

这是我的代码:

+ (UIImage *) drawColorTriangleWithBorderWidth:(int)Width Height:(int)Height R:(int)R G:(int)G B:(int)B A:(int)A BorderR:(int)BR BorderG:(int)BG BorderB:(int)BB BorderA:(int)BA Thickness:(CGFloat)thickness {

    if ( Width == 0 || Height == 0) return nil;

    float de = thickness/2.0;

    UIGraphicsBeginImageContext(CGSizeMake(Width+de, Height+de));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);    

    CGFloat rr = 1.0*R/255.0; if ( rr > 1.0) rr = 1.0;
    CGFloat gg = 1.0*G/255.0; if ( gg > 1.0) gg = 1.0;
    CGFloat bb = 1.0*B/255.0; if ( bb > 1.0) bb = 1.0;
    CGFloat aa = 1.0*A/100.0; if ( aa > 1.0) aa = 1.0;

    CGFloat brr = 1.0*BR/255.0; if ( brr > 1.0) brr = 1.0;
    CGFloat bgg = 1.0*BG/255.0; if ( bgg > 1.0) bgg = 1.0;
    CGFloat bbb = 1.0*BB/255.0; if ( bbb > 1.0) bbb = 1.0;
    CGFloat baa = 1.0*BA/100.0; if ( baa > 1.0) baa = 1.0;

    CGContextSetLineCap(context, kCGLineCapSquare);
    CGContextSetRGBFillColor(context, rr, gg, bb, aa);
    CGContextSetLineWidth(context, thickness);
    CGContextSetRGBStrokeColor(context, brr, bgg, bbb, baa);

    CGContextMoveToPoint(context, Width/2.0, de);
    CGContextAddLineToPoint(context, de, Height-de);
    CGContextAddLineToPoint(context, Width-de, Height-de);
    CGContextAddLineToPoint(context, Width/2.0, de);
    CGContextDrawPath(context, kCGPathFillStroke);

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

谁能帮帮我? 谢谢。

【问题讨论】:

  • 注意:CGFloat rr = 1.0*R/255.0; if ( rr > 1.0) rr = 1.0;可以更简单的写成:CGFloat rr = MIN(R/255.0, 1.0);
  • 不要使用最后一个CGContextAddLineToPoint,而是使用CGContextClosePath
  • 为什么要使用大写的变量?

标签: ios colors geometry fill


【解决方案1】:

添加不同路径后需要关闭路径:

CGContextMoveToPoint(context, Width/2.0, de);
CGContextAddLineToPoint(context, de, Height-de);
CGContextAddLineToPoint(context, Width-de, Height-de);
CGContextClosePath(context);
CGContextFillPath(context);

【讨论】:

  • CGContextFillPath() 文档明确指出:“Quartz 将每个子路径视为通过调用 CGContextClosePath 关闭了它。” - 因此不需要关闭路径。
  • 好的,无论如何都需要填充
  • 如果你想填充路径,你必须使用CGContextDrawPath(..., kCGPathFillStroke)(比较stackoverflow.com/a/16462849/1187415)。
【解决方案2】:

这可以使用 OP 发布的代码:

UIImage *image = [[self class] drawColorTriangleWithBorderWidth:100 Height:100 R:200 G:0 B:0 A:200 BorderR:0 BorderG:0 BorderB:200 BorderA:100 Thickness:3.0];

[UIImageJPEGRepresentation(image, 1.0) writeToFile:@"/Users/dan/Desktop/UIImage.jpg" atomically:YES];

制作此图片:

【讨论】:

  • CGContextFillPath() 文档明确指出:“Quartz 将每个子路径视为通过调用 CGContextClosePath 关闭它。” - 因此不需要关闭路径。
  • @Martin 但是如果我画出三角形的两条边并进行描边,我是否也会得到第三条边?我的阅读是 CGContextClosePath 不需要填充,完整的引用仅在填充例程的文档中。这是否意味着不需要添加三角形的第三条边进行填充?
  • 首先,如果你想填充描边路径,你必须使用CGContextDrawPath(context, kCGPathFillStroke)(比较stackoverflow.com/a/16462849/1187415)。你是对的,关闭路径对于完整的抚摸是必要的。但是问题中报告的问题是“三角形周围的整个区域也被填充了”。与CGContextFillPath() 相比,kCGPathFillStroke 可能不会隐式关闭路径...
猜你喜欢
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
相关资源
最近更新 更多