Jon*_*123 0 transparency image core-graphics ios uibezierpath
我有一个jpg图像与矩形中的圆形对象,并希望使圆形对象的envoirement透明...

(在此示例中删除红色区域)
在这个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;
Run Code Online (Sandbox Code Playgroud)
有人解决了吗?
要仅在黑色圆圈内绘制,您应该移动[_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;
Run Code Online (Sandbox Code Playgroud)