gpt4 book ai didi

iphone - CGContextClearRect 带圆圈

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:53 25 4
gpt4 key购买 nike

我正在创建一个应用程序,我试图在其中清除 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();

最佳答案

我不确定你在做什么,但你的剪切弧没有被正确创建。您是在固定位置创建它,这就是为什么它实际上不起作用的原因 - 除了它起作用,如果您单击左上角。

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

- (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尝试

关于iphone - CGContextClearRect 带圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14755840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com