我想将顶部偏移量添加到CGContextFillEllipseInRect

当我使用topOffset等于零时,一切正常。但是,当我不使用零偏移量时,圈错了。
我尝试设置CGContextTranslateCTM(context, 0, topOffset);,但结果相同。

float topOffset = 30.f;
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *img = [UIImage imageNamed:@"Circle.png"];
CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:img] CGColor]);
CGContextFillEllipseInRect(context, CGRectMake(0, topOffset, img.size.width, img.size.height));

无偏移

顶部偏移

最佳答案

CGContextSetFillColorWithColor方法准备填充给定颜色值的整个上下文区域。

调用Stroking or Filling Method时,裁剪给定的笔触或填充区域。

因此,您将获得Ellipse或Rect或类似的内容...

  • 首先,调用CGContextSetFillColorWithColor设置填充颜色(或图案颜色)和FillRect整个区域。


  • 其次,调用CGContexetSetFillColorWithColor设置图案并使用顶部,左侧偏移量填充


  • 背景相同,但裁剪给定的FillRect()和FillEllipseInRect()区域,

    就你而言...
  • 调用CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:img] CGColor]);并填充整个区域


  • ,当您用“顶部偏移”填充“椭圆”时。


    这就是为什么您的圈子看起来不对。

    如果要使用单一填充颜色,请尝试使用CGContextSetRGBFillColor设置填充颜色。

    编辑,尝试执行此操作(在FillColor之前更改CTM,在Ellipse Drawing之前恢复Restore)
    float topOffset = 30.f;
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage *img = [UIImage imageNamed:@"Circle.png"];
    CGContextTranslateCTM(context, 0, topOffset);
    CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:img] CGColor]);
    CGContextTranslateCTM(context, 0, (-1*topOffset);
    CGContextFillEllipseInRect(context, CGRectMake(0, topOffset, img.size.width, img.size.height));
    

    对不起,英语不好:)

  • 10-08 04:16