3

をクリアしようとしているアプリを作成してい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();
4

1 に答える 1

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のサンプル プロジェクトで確認できます。ダウンロードして試してみることができます。

于 2013-02-08T09:51:46.080 に答える