CGContextClearRect with circle

I am creating an app in which i am trying to clear the rect of UIImageView. I have achieved this with CGContextClearRect, but the problem is that it is clearing rect in square shape and i want to achieve this effect in round shape.

What i have tried so far:

UITouch *touch2 = [touches anyObject];
CGPoint point = [touch2 locationInView:img];

UIGraphicsBeginImageContextWithOptions(img.bounds.size, NO, 0.0f);
[img.image drawInRect:CGRectMake(0, 0, img.frame.size.width, img.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();

CGRect cirleRect = CGRectMake(point.x, point.y, 40, 40);
CGContextAddArc(context, 50, 50, 50, 0.0, 2*M_PI, 0);
CGContextClip(context);
CGContextClearRect(context,cirleRect);
//CGContextClearRect(context, CGRectMake(point.x, point.y, 30, 30));
img.image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
share improve this question
 
1 
Check this answer, may this will work for you. [1]: stackoverflow.com/questions/6061200/… –  sandy  Feb 8 '13 at 7:58 
 
Check this answer with a running example that can help: stackoverflow.com/a/27796085/1381708 –  Gabriel.Massana  Jan 6 '15 at 9:54 

1 Answer

up vote 2 down vote accepted

I'm not sure exactly what you are doing, but your clipping arc isn't being created correctly. You are creating it at a fixed position which is why it doesn't really work - except it does, if you click in the top left corner.

If you want to see it working try this:

- (IBAction)clearCircle:(UITapGestureRecognizer *)sender {
    UIImageView *imageView = self.imageView;
    UIImage *currentImage = imageView.image;
    CGSize imageViewSize = imageView.bounds.size;

    CGFloat rectWidth = 40.0f;
    CGFloat rectHeight = 40.0f;

    CGPoint touchPoint = [sender locationInView:imageView];

    UIGraphicsBeginImageContextWithOptions(imageViewSize, YES, 0.0f);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [currentImage drawAtPoint:CGPointZero];

    CGRect clippingEllipseRect = CGRectMake(touchPoint.x - rectWidth / 2, touchPoint.y - rectHeight / 2, rectWidth, rectHeight);
    CGContextAddEllipseInRect(ctx, clippingEllipseRect);

    CGContextClip(ctx);

    CGContextClearRect(ctx, clippingEllipseRect);

    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}

Which creates a clipping ellipse (in this case a circle) in the rect 40 x 40 centred at the touch point.

You can see this in an example project on Bitbucket which you can download for yourself to try

share improve this answer
 
 
Its working, but the problem is that there is black color behind image. And i have another image behind image, not black color? so what's this happening –  EXC_BAD_ACCESS  Feb 8 '13 at 11:29
1 
That's a completely different question - but as a hint, you should look at image masks. –  Abizern  Feb 8 '13 at 11:44
 
CGContextClip(ctx); Saved my life! –  Albert Renshaw  Sep 24 '14 at 23:16
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值