我想将顶部偏移量添加到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或类似的内容...
背景相同,但裁剪给定的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));
对不起,英语不好:)