为什么当我在笔画路径颜色之前填充路径颜色时,CGContext不会绘制任何内容。

时间:2015-07-07 04:09:03

标签: ios cgcontext cgpath

像这样的代码

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 5, 5);
CGContextAddLineToPoint(context, 100, 100);
CGContextSetLineWidth(context, 5);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextFillPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
CGContextStrokePath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

当我删除填充颜色时,图像是正确的。 但我无法理解为什么我添加填充颜色图像什么都没有?

1 个答案:

答案 0 :(得分:0)

问题在于你如何利用上下文。

请尝试以下方法:

    CGRect rect = CGRectMake(0.0,0.0, 100, 100);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //Set background color
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context,rect);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextMoveToPoint(context, 5, 5);
    CGContextAddLineToPoint(context, 100, 100);
    CGContextStrokePath(context);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

现在

 _testImage.image = image;

你会得到结果。请告诉我您的反馈意见。

相关问题