问题
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