iOS:反向UIBezierPath(bezierPathWithOvalInRect) [英] iOS: Inverse UIBezierPath (bezierPathWithOvalInRect)

查看:1455
本文介绍了iOS:反向UIBezierPath(bezierPathWithOvalInRect)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有圆形物体的jpg图像,并希望使圆形物体的透明度变得透明......

I have an jpg Image with a round object in a rect and want to make the envoirement of the round object transparent...

(在此示例中删除红色区域)

(Remove red area in this example)

在此 iOS的帮助下一个UIImage透明和UIBezierPath bezierPathWithOvalInRect我取得了一些成功,但是它围绕我的对象创建了一条路径,我需要将其反转,以使外部而不是路径内部透明。

With the help of this iOS make part of an UIImage transparent and "UIBezierPath bezierPathWithOvalInRect" i've got a little success, but this make a path around my object, and I need to invert it, to make the outside and not the inside of the path transparent..

我不得不改变我的代码以获得正确的结果..

Im not shure where I have to change my code to get the right result..

这是我的代码:

//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;

有人解决了吗?

推荐答案

要仅在黑色圆圈内绘制,你应该将 [_ imgTemp.image drawAtPoint:CGPointZero]; 移到 CGContextClip之后(context); 然后完全删除 CGContextClearRect(...)调用:

To only draw within the black circle you should move [_imgTemp.image drawAtPoint:CGPointZero]; to after CGContextClip(context); and then remove the CGContextClearRect( ... ) call entirely:

//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;

这篇关于iOS:反向UIBezierPath(bezierPathWithOvalInRect)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆