使用UIImage填充UIBezierPath

Kri*_*sen 6 cocoa objective-c

我有一个圆形的UIBezierpath:

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:100];
Run Code Online (Sandbox Code Playgroud)

但后来我想用UIImage填充圆圈(仅显示圆圈内的图像的一部分)

最诚挚的问候克里斯蒂安

编辑:

感谢Daniel和Dave的出色答案:D给我带来了很多麻烦; D

解决方案:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddPath(ctx, path.CGPath);
CGContextClip(ctx);
Run Code Online (Sandbox Code Playgroud)

和:

path.addClip;
Run Code Online (Sandbox Code Playgroud)

两者都很完美,但我最终使用了最后一种方法(由戴夫),因为它需要更少的代码.

Dan*_*son 9

更新:正如Dave DeLong所指出的,没有必要使用Core Graphics功能.这应该做的伎俩:

[path addClip];
[image drawAtPoint:CGPointZero];
Run Code Online (Sandbox Code Playgroud)

  • +1,虽然你不需要放弃CoreGraphics来做到这一点.您可以使用` - [UIImage draw ....]`和` - [UIBezierPath addClip]`来执行此操作. (2认同)