以下方法应创建一个FILLED三角形图像,但它只创建一个轮廓。 为什么不填?和这个人一起疯狂。我希望它得到回应,以便下一个与之斗争的可怜的灵魂可以清除障碍并挽救他们生命中的一小时。
这是我写的方法:
+ (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;
}
感谢任何帮助。
答案 0 :(得分:2)
我从以下帖子中找到答案: CGContext line drawing: CGContextFillPath not working?
最后一个答案之一提供了使用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);
我不知道为什么我上面使用的原始方法不起作用。我接下来会想到这一点。