ios - How to get the intersection of two CGPath? -
i using cashapelayer.path
, calayer.mask
set image mask, can achieve "difference set" , "union" effect cgpath
setting
masklayer.fillrule = kcafillruleevenodd/kcafillrulezero
however, how can intersection of 2 paths? cannot achieve even-odd rule
here example:
let view = uiimageview(frame: cgrectmake(0, 0, 400, 400)) view.image = uiimage(named: "scene.jpg") let masklayer = cashapelayer() let maskpath = cgpathcreatemutable() cgpathaddellipseinrect(maskpath, nil, cgrectoffset(cgrectinset(view.bounds, 50, 50), 50, 0)) cgpathaddellipseinrect(maskpath, nil, cgrectoffset(cgrectinset(view.bounds, 50, 50), -50, 0)) masklayer.path = maskpath masklayer.fillrule = kcafillruleevenodd masklayer.path = maskpath view.layer.mask = masklayer
how can middle part of shown? mean, middle part(which blank in sample view).
i can in vector design software ai, think there must way handle.
extension uiimage { func doublecirclemask(#circlesize:cgfloat)-> uiimage { let rectangle = cgrectmake(0, 0, size.width, size.height) uigraphicsbeginimagecontext(size) let x = size.width/2-circlesize/2 let y = size.height/2-circlesize/2 let offset = circlesize/4 let shape1 = uibezierpath(roundedrect: cgrectmake(x-offset, y, circlesize, circlesize), cornerradius: circlesize/2) shape1.appendpath(uibezierpath(roundedrect: cgrectmake(x+offset, y, circlesize, circlesize), cornerradius: circlesize/2)) shape1.addclip() drawinrect(rectangle) let result = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return result } func doublecircleintersectionmask(#circlesize:cgfloat)-> uiimage { let rectangle = cgrectmake(0, 0, size.width, size.height) uigraphicsbeginimagecontext(size) let x = size.width/2-circlesize/2 let y = size.height/2-circlesize/2 let offset = circlesize/4 let shape1 = uibezierpath(roundedrect: cgrectmake(x-offset, y, circlesize, circlesize), cornerradius: circlesize/2) let shape2 = uibezierpath(roundedrect: cgrectmake(x+offset, y, circlesize, circlesize), cornerradius: circlesize/2) shape1.addclip() shape2.addclip() drawinrect(rectangle) let result = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return result } }
testing
let mypicture = uiimage(data: nsdata(contentsofurl: nsurl(string: "http://i.stack.imgur.com/xs4rx.jpg")!)!)! let mypictureunion = mypicture.doublecirclemask(circlesize: 300) let mypictureintersection = mypicture.doublecircleintersectionmask(circlesize: 300)
Comments
Post a Comment