ios - 为什么当我在描边路径颜色之前填充路径颜色时 CGContext 不绘制任何内容。

标签 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();

当我删除填充颜色时,图像是正确的。 但是我不明白为什么我添加了填充颜色图像什么都没有?

最佳答案

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

请尝试以下操作:

    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;

你会得到结果。请让我知道您的反馈。

关于ios - 为什么当我在描边路径颜色之前填充路径颜色时 CGContext 不绘制任何内容。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31259588/

相关文章:

ios - 使用 Cglayer 绘图进行撤消和重做

ios - 如何让我的应用出现在 App Store 搜索中?

iphone - 在 iOS 上创建在线数据库的本地缓存

ios - 使用 UISearchController 按对象属性搜索?

ios - CALayer drawrect 抗锯齿功能不起作用

objective-c - 如何使用 CATransform3D 为一组 CALayer 添加阴影

iOS 8 :custom Keyboard with undo and redo button

iphone - 使用CGContext时的图像绘制问题。图像水平镜像

ios - 如何加入圆角线

ios - 我如何在 CGPathGetCurrentPoint 添加圆弧?