使用自定义UIBezierPath剪切图像 [英] Clipping an image with a custom UIBezierPath

查看:177
本文介绍了使用自定义UIBezierPath剪切图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个用户创建的 UIBezierPath

我可以通过使用<$ c $来创建bog标准UIView上的形状c> [myPath fill]; function。



我最想做的是使用路径创建剪贴蒙版UIImage - 也许作为一个 UiImageView



我试图将我的UIView更改为 UIImageView 然后将函数更改为 [myPath addClip]; ,但由于某种原因它不工作。

解决方案

您需要创建一个图形上下文,绘制图像,并使用kCGBlendModeDestinationIn绘制掩码。像这样:

  UIGraphicsBeginImageContextWithOptions(size,NO,0.0); // size是你想要的结果的CGSize 
[image drawInRect:imageRect]; // image是你想要剪辑的UIImage,imageRect是你想要你的图像的上下文中的rect
[mask drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0]; // mask是一个包含你绘制的掩码的UIImage,rect是你想要的掩码的上下文中的rect - 可以匹配上面的imageRect
UIImage * uiImage = UIGraphicsGetImageFromCurrentImageContext();


I was wondering if someone could point me in the right direction with this problem.

I have a user-created UIBezierPath with several points that have come about by user touches.

I can get these to create shapes on a bog-standard UIView by using the [myPath fill]; function.

What I would ideally like to do is to use the path to create a clipping mask for a UIImage - perhaps as a UiImageView?

I tried changing my UIView to a UIImageView and then changing the function to [myPath addClip]; but for some reason it does not work.

解决方案

You need to create a graphics context, draw your image, and draw your mask using kCGBlendModeDestinationIn. Something like this:

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); // size is a CGSize of the result you want
[image drawInRect:imageRect];  // image is the UIImage you want to clip, imageRect is the rect in the context where you want your image
[mask drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0];  // mask is a UIImage containing the mask you drew, rect is the rect in the context where you want your mask - could match imageRect above
UIImage* uiImage = UIGraphicsGetImageFromCurrentImageContext();

这篇关于使用自定义UIBezierPath剪切图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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