How to draw CALayer border around its mask?

后端 未结 6 1449
谎友^
谎友^ 2021-02-07 09:39

So, I have a CALayer, which has a mask & I want to add border around this layer\'s mask. For example, I have set triangle mask to the layer and I want to have b

6条回答
  •  孤独总比滥情好
    2021-02-07 09:49

    Some suggestions:

    • Use an opaque shadow instead of a border (you will have a blurred effect).
    • Create another layer, set its background color with the color you want for your border, mask it with a mask slightly bigger than the one you already have to simulate the border width, and put it centered behind your layer (may not work with every shape).
    • Do a morphological operation on your mask image to calculate the border, for instance with the vImageDilate family of functions (more complicated, and may run into performance problems).
    • If you know the shape and it can be described mathematically, draw it and stroke it explicitly with Core Graphics functions.
    • Or, in the same case (shape known mathematically), use a CAShapeLayer to draw the border.

提交回复
热议问题