Put border around partially transparent Image being drawn on CGContext

前端 未结 2 582
囚心锁ツ
囚心锁ツ 2020-12-23 10:50

I have an image with a yellow vase in the foreground and transparent background:

\"enter

相关标签:
2条回答
  • 2020-12-23 11:18

    One way to do this is to use the mathematical morphology operator of dilation to "grow" the alpha channel of the image outward, then use the resulting grayscale image as a mask to simulate a stroke. By filling the dilated mask, then drawing the main image on top, you get the effect of a stroke. I've created a demo showing this effect, available on Github here: https://github.com/warrenm/Morphology (all source is MIT licensed, should it prove useful to you).

    And here's a screenshot of it in action:

    enter image description here

    Note that this is staggeringly slow (dilation requires iteration of a kernel over every pixel), so you should pick a stroke width and precompute the mask image for each of your source images in advance.

    0 讨论(0)
  • 2020-12-23 11:19

    I would try either setting the stroke color and line width before your call to CGContextDrawImage, or tweaking the shadow (opacity, blur, etc) so that it looks like a stroke around the image. Let me know if this works!

    0 讨论(0)
提交回复
热议问题