在 iOS 中使用图像进行遮罩
我想拍摄一张图像并将其用作我添加大量图像视图的视图的蒙版。我知道石英 CGContextClipToMask() 调用,但是解决这个问题的最佳方法是什么?我可以重写容器视图的drawRect方法,在其中调用CGContextClipToMask(),然后期望其子视图遵守该剪切区域吗?这似乎不起作用。
我是否需要在顶部添加一些遮挡蒙版图像?
I'd like to take an image and use it as a mask for a view on which I add numerous image views. I know of the quartz CGContextClipToMask() call but what would be the best way to approach this? Can I override the drawRect method of a container view, call CGContextClipToMask() within it, and then expect its subviews to adhere to that clipping region? It doesn't seem to work.
Do I need to instead add some blocking mask image over top?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我选择使用可见部分具有透明度的图像来覆盖图像,而不是子类化或覆盖绘图函数。即,如果我的“表面”是一张羊皮纸的图像,并且我的目标是在上面绘制一堆图像。我将拥有羊皮纸图像,然后是一个用于将任何图像放在羊皮纸上的容器 UIView,然后是原始羊皮纸图像上方的遮罩图像,但羊皮纸本身转换为完全透明,而周围区域与羊皮纸所在的背景完全相同(然后是所有其他 UI 小部件)。
在所有情况下,这似乎都是一种可行的解决方案,除非需要一些图像在羊皮纸周围和后面进行视觉动画(不是我的情况)。
Instead of subclassing or overriding drawing functions, I chose to overlay the images with an image that had transparency in the viewable portion. i.e., if my 'surface' was an image of a parchment, and I aimed to draw a bunch of images on it. I would have the parchment image, then a container UIView for any images to be put on that parchment, then a masking image over top of that which was the original parchment image but with the parchment itself converted instead to full transparency, while the surrounding area is left exactly as the background the parchment is on (then all other UI widgets over top of that).
This seems a viable solution in all cases except if one were to need some image to visually animate around and behind the parchment (not my case).