我正在创建一个应用,我正在尝试清除rect
UIImageView
。我用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();
答案 0 :(得分:2)
我不确定你到底在做什么,但你的剪裁弧没有正确创建。你是在一个固定的位置创建它的,这就是为什么它不能真正起作用 - 除非你点击左上角。
如果你想看到它正常运行,试试这个:
- (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中创建剪裁椭圆(在本例中为圆形)。
您可以在Bitbucket上的示例项目中看到这一点,您可以download for yourself尝试