Reputation: 3274
I've looked through various similar questions and still can't seem to get my code to work. I have a UIView with an image drawn to it ([image drawInRect:bounds]
), but I'm missing something in my context clipping:
// Get context & bounds, and calculate centre & radius
CGContextRef ctx=UIGraphicsGetCurrentContext();
CGRect bounds=[self bounds];
CGPoint centre;
centre.x=bounds.origin.x+0.5*bounds.size.width;
centre.y=bounds.origin.y+0.5*bounds.size.height;
CGFloat radius=centre.x;
// Draw image
UIImage *backImage=[UIImage imageNamed:@"backimage.png"];
[backImage drawInRect:bounds];
// Create clipping path and clip context
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, centre.x, centre.y, radius, 0, 2*M_PI, 0);
CGContextAddPath(ctx, path);
CGContextClip(ctx);
Any ideas of where I went wrong? Thanks for reading.
Upvotes: 2
Views: 3860
Reputation: 539775
radius=centre.x
seems to be wrong. The radius should be half the width or height:
CGFloat radius = 0.5*bounds.size.width;
You could also use the convenience function
CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
UPDATE: It turned out that the actual problem was that the clipping path was modified after drawing the image.
The clipping path is used for all future drawing operations and must therefore be set before the drawing.
Upvotes: 4