【问题标题】:Drawing path stroke and shadow (iOS)绘制路径描边和阴影(iOS)
【发布时间】:2011-06-29 17:29:31
【问题描述】:

我正在使用此代码创建圆角图像:

CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, imageObj.size.width, imageObj.size.height);

    CGContextAddRect(context, rect);


CGContextSaveGState(context);
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM (context, ovalWidth, ovalHeight);
fw = CGRectGetWidth (rect) / ovalWidth;
fh = CGRectGetHeight (rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2);
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
CGContextRestoreGState(context);

CGContextClosePath(context);
CGContextClip(context);

CGContextDrawImage(context, CGRectMake(0, 0, w, h), imageObj.CGImage);

如何添加描边和阴影?

到目前为止对我没用...

谢谢!

【问题讨论】:

  • 路径绘制代码绘制的是椭圆形,而不是圆角矩形。对于圆角矩形,您需要选择一个角半径,然后将该半径用作每个圆弧的半径。从一个角到下一个角的直线将为您包括在内。

标签: ios drawing core-graphics quartz-graphics


【解决方案1】:

您必须执行 CGContextStrokePath 或 CGContextFillPath 来绘制您创建的路径。 (确保你先用 CGContextSetStrokeColor 设置描边颜色)然后你可以使用 CGContextSetShadow 来制作阴影,这里是一个苹果开发者页面描述 CoreGraphics 阴影:

http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadows/dq_shadows.html

【讨论】:

  • 谢谢,我试过没有成功,它没有划清界限...请详细说明如何做到这一点?
  • @Asaaf b 您尝试从 CGContextClip 绘制的路径是否可能被剪裁
  • @Assaf b:我注意到你做了一个翻译和缩放,但是你没有做任何事情就 grestore。平移和缩放步骤是不必要且错误的,因为您随后自己计算转换后的坐标,因此它们是双重转换的:如果您要实际绘制,您的绘图将在错误的位置并且太大。此外,CGContextAddRect 不会给你任何圆角的东西。扔掉AddRect,翻译和缩放;在CGPath对象中绘制路径,然后保存,添加路径,设置阴影,描边,然后grestore,重新添加路径,剪辑,绘制图像。
猜你喜欢
  • 2013-07-04
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2010-11-16
  • 2014-03-01
  • 1970-01-01
相关资源
最近更新 更多