6

I got an UIBezierpath with a circle shape:

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:100];

But then i want to fill the circle with an UIImage (show only a portion of the image which is inside the circle)

Best Regards Kristian

Edit:

Thanks to Daniel and Dave for excellent answers :D saved me a lot of trouble ;D

The solutions:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddPath(ctx, path.CGPath);
CGContextClip(ctx);

and:

path.addClip;

Both works perfectly but i ended up using the last method (by Dave) because it required less code.

Community
  • 1
  • 1

1 Answers1

9

Update: as Dave DeLong points out, there's no need to use Core Graphics functions. This should do the trick:

[path addClip];
[image drawAtPoint:CGPointZero];
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • 2
    +1, though you don't need to drop to CoreGraphics to do this. You can use `-[UIImage draw....]` and `-[UIBezierPath addClip]` to do this. – Dave DeLong Mar 09 '11 at 18:15
  • Good call Dave -- I hadn't noticed the `addClip` method. Updated the answer showing the UIKit implementation. – Daniel Dickison Mar 09 '11 at 21:14