【问题标题】:Draw image into area between two concentric circles in Xcode / objective-c在Xcode / Objective-c中将图像绘制到两个同心圆之间的区域
【发布时间】:2012-03-03 20:58:08
【问题描述】:

我有一个图像(它实际上是圆形的)并且希望只绘制该图像中与两个同心圆之间的区域相对应的部分,外部是固定的,内部可以调整大小。

外同心圆将始终对应于图像的外边缘 - 但需要在绘制的图像中留下一个“洞” - 对应于较小同心圆的大小。

我正在尝试在 iOS 的 Objective-c 中执行此操作 - 以绘制可变大小的“取景器”控件。

非常感谢任何帮助。

【问题讨论】:

  • FWIW 是一个内部圆盘被切掉的圆盘的术语,是一个“环”。

标签: objective-c image cocoa-touch geometry masking


【解决方案1】:

听起来像是使用CGContextEOClip 的标准情况。没有检查此代码,但类似:

CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, outerCircle);
CGPathAddEllipseInRect(path, NULL, innerCircle);
CGContextEOClip(ctxt);
//Draw your stuff

【讨论】:

  • 您需要使用CGContextEOClip
  • 固定使用CGContextEOClip
【解决方案2】:

最简单和最高效的解决方案可能是使用UIImageView 并应用CAShapeLayer 作为其图层的掩码。

例子:

UIImage *image = ...;
CGFloat ringWidth = 20.0;
CGRect outerCircleRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGRect innerCircleRect = CGRectInset(outerCircleRect, ringWidth, ringWidth);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:outerCircleRect];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:innerCircleRect]];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.fillRule = kCAFillRuleEvenOdd;
shapeLayer.path = [path CGPath];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
imageView.layer.mask = shapeLayer;
[self.view addSubview:imageView];

您必须包含 QuartzCore 框架才能使其工作。

当你改变内圆的半径时,你可以简单地为形状层分配一个新的路径。这甚至可以是动画。

【讨论】:

  • 非常感谢,omz!这很好用。我的几何形状有点生疏(非常生疏),所以这是一个很大的帮助。彼得
猜你喜欢
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多