Clipping an image with a custom UIBezierPath

こ雲淡風輕ζ 提交于 2019-12-20 04:06:11

问题


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.


回答1:


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();


来源:https://stackoverflow.com/questions/8783631/clipping-an-image-with-a-custom-uibezierpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!