CGContextClearRect与圆形

我正在创build一个应用程序,我正在尝试清除UIImageViewrect 。 我已经用CGContextClearRect实现了这个,但问题是它是正方形清理rect ,我想实现圆形的这种效果。

我到目前为止所尝试的:

 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(); 

我不确定你在做什么,但你的剪切弧没有被正确创build。 你在一个固定的位置创build它,这就是为什么它不工作 – 除了它,如果你点击左上angular。

如果你想看到它的工作尝试这个:

 - (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(); } 

它在以触摸点为中心的矩形40 x 40中创build一个裁剪椭圆(本例中为一个圆)。

你可以在Bitbucket的一个示例项目中看到这个,你可以自己下载试试