0

我有一个 jpg 图像,矩形中有一个圆形物体,并且想让圆形物体的环境透明......

例子

(去除本例中的红色区域)

在这个iOS 的帮助下,使 UIImage 的一部分变得透明和“UIBezierPath bezierPathWithOvalInRect”我取得了一点成功,但这在我的对象周围创建了一条路径,我需要反转它,以使外部而不是内部路径透明..

我不确定我必须在哪里更改我的代码才能获得正确的结果..

这是我的代码:

//BezierPath
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];  

// Create an image context containing the original UIImage.
UIGraphicsBeginImageContext(_imgTemp.image.size);
[_imgTemp.image drawAtPoint:CGPointZero];

// Clip to the bezier path and clear that portion of the image.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context,bezierPath.CGPath);
CGContextClip(context);
CGContextClearRect(context,CGRectMake(0,0,self._imgTemp.image.size.width,self._imgTemp.image.size.height));

// Build a new UIImage from the image context.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self._imgTemp.image = newImage;

有人解决了吗?

4

1 回答 1

9

要仅在黑色圆圈内绘制,您应该移至[_imgTemp.image drawAtPoint:CGPointZero];之后CGContextClip(context);,然后CGContextClearRect( ... )完全删除呼叫:

//BezierPath
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];  

// Create an image context containing the original UIImage.
UIGraphicsBeginImageContext(_imgTemp.image.size);

// Clip to the bezier path and clear that portion of the image.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context,bezierPath.CGPath);
CGContextClip(context);

// Draw here when the context is clipped
[_imgTemp.image drawAtPoint:CGPointZero];

// Build a new UIImage from the image context.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self._imgTemp.image = newImage;
于 2012-04-06T10:31:34.777 回答