为圆形路径中绘制的 UIImage 添加阴影
我正在 UITableView 单元格中绘制圆形图像,如下所示:
CGRect imageRect = CGRectMake(8, 8, 40, 40);
CGFloat radius = 3;
CGFloat minx = CGRectGetMinX(imageRect);
CGFloat midx = CGRectGetMidX(imageRect);
CGFloat maxx = CGRectGetMaxX(imageRect);
CGFloat miny = CGRectGetMinY(imageRect);
CGFloat midy = CGRectGetMidY(imageRect);
CGFloat maxy = CGRectGetMaxY(imageRect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextClip(context);
[self.theImage drawInRect:imageRect];
这看起来很棒,但我想为其添加阴影以增加效果。 我尝试过使用以下内容:
CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 2, [[UIColor grayColor] CGColor]);
CGContextFillPath(context);
但这仅在图像具有透明区域时有效,如果图像根本不透明,它甚至不会在边框周围绘制阴影。
我想知道我是否做错了什么?
I'm drawing a rounded image in a UITableView cell like so:
CGRect imageRect = CGRectMake(8, 8, 40, 40);
CGFloat radius = 3;
CGFloat minx = CGRectGetMinX(imageRect);
CGFloat midx = CGRectGetMidX(imageRect);
CGFloat maxx = CGRectGetMaxX(imageRect);
CGFloat miny = CGRectGetMinY(imageRect);
CGFloat midy = CGRectGetMidY(imageRect);
CGFloat maxy = CGRectGetMaxY(imageRect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextClip(context);
[self.theImage drawInRect:imageRect];
This looks great, but I'd like to add a shadow to it for added effect.
I've tried using something along the lines of:
CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 2, [[UIColor grayColor] CGColor]);
CGContextFillPath(context);
But this only works when the image has transparent areas, if the image isn't transparent at all, it won't even draw a shadow around the border.
I'm wondering if there is something I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,正确的方法是剪切 UIImage 本身而不是路径。那么这就是一个简单的问题:
Turns out the correct way to do this is by clipping the UIImage itself rather than the path. It's then a simple matter of:
我想如果您要绘制的图像没有透明区域,那么 CoreGraphics 会将整个图像解释为实体对象。然后,如果它尝试绘制阴影,阴影将绘制在图像边界之外,因此您将看不到它。
此页面提供了一个很好的示例介绍如何使用 CoreGraphics 进行此类绘图。也许您可以尝试修改那里提供的代码,看看是否可以解决您的问题。
I'd imagine that if the image you're drawing to has no transparent areas then CoreGraphics interprets the whole image as a solid object. Then if it tries to draw a shadow the shadow will be draw outside the bounds of the image and as such you will not see it.
This page provides a good example of how to do such drawing with CoreGraphics. Perhaps you could try modifying the code provided there and see if that solves your problem.