Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What you are asking for is the union of bezier paths. Apple doesn’t ship any APIs for computing the union of paths. It is in fact a rather complicated algorithm. Here are a couple of links:
If you explain what you want to do with the union path, we might be able to suggest some alternatives that don’t require actually computing the union.
You can draw a pretty decent inner glow without actually computing the union of the paths. Instead, make a bitmap. Fill each path on the bitmap. You’ll use this as the mask. Next, create an inverted image of the mask, that has everything outside of the union area filled. You’ll draw this to make CoreGraphics draw a shadow around the inner edge of the union. Finally, set the mask as your CGContext mask, set the shadow parameters, and draw the inverted image.
Ok, that sounds complicated. But here’s what it looks like (Retina version on the right):
It’s not perfect (too light at the corners), but it’s pretty good.
So here’s the code. I’m passing around UIBezierPaths instead of CGPaths, but it’s trivial to convert between them. I use a some UIKit functions and objects. Remember that you can always make UIKit draw to an arbitrary CGContext using
UIGraphicsPushContextandUIGraphicsPopContext.First, we need an mask image. It should be an alpha-channel-only image that is 1 inside any of the paths, and 0 outside all of the paths. This method returns such an image:
That was actually the hard part. Now we need an image that is our glow color everywhere outside of the mask (path union) area. We can use UIKit functions to make this easier than a pure CoreGraphics approach:
With those two images, we can draw an inner glow into the current UIKit graphics context for an array of paths:
To test it, I created an image using a couple of circles and put it in a UIImageView: