iOS layer mask from UIImage/CGImage?

蓝咒 提交于 2019-12-11 22:39:24

问题


I have the following image:

On a UIImageView of the exact same frame size, I want to show everything but the red fill.

        let mask = CALayer()
        mask.contents = clippingImage.CGImage
        self.myImageView.layer.mask = mask

I thought the black color would show through when applied as a mask, but when I set the mask the whole view is cleared. What's happening here?


回答1:


When creating masks from images, you use the Alpha channel, rather than any RGB channel. Even if your mask is black, you need to set its Alpha value to 0, as the mask pays attention only to Alpha channel. Any by default black is [0,0,0,255] in terms of RGBA. If you load RGB, it will of course convert it into RGBA with A = 1.



来源:https://stackoverflow.com/questions/33004148/ios-layer-mask-from-uiimage-cgimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!