iOS 中的石英掩模——它们还会导致崩溃吗?
根据 2008 年的这个问题,使用石英掩模可能会导致崩溃!现在还是这样吗?
基本上,我想做的是在固定背景上绘制不同颜色的骰子,为每个骰子形状使用一个 png(有很多),并以某种方式在代码中添加颜色。
编辑:澄清一下,例如我想使用一个 png 文件来制作以下所有内容:
基本上, 我想要将图像的红色、绿色和蓝色分量乘以三个独立的常数,同时保持 alpha 不变。
According to this question from 2008, using quartz masks can cause crashes! Is that still the case?
Basically, what I want to do is to draw dice of different colors on a fixed background, using one png for each die shape (there are a lot of them), and somehow add the colors in code.
EDIT: to clarify, for example I want to use one png file to make all of the following:
Basically, I want to multiply the red, green, and blue components of my image by three independent constants, while leaving the alpha unchanged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个镜头。经测试,无泄漏。没有崩溃。
.h
.m
我用两个 UIImageView 和一个 UIButton 设置了 xib。顶部的 UIImageView 已通过 Interface Builder 预加载了图像。触摸文本按钮,图像将被处理并设置到第二个 UIImageView。
顺便说一句,我在从帖子中复制你的图标时遇到了一些麻烦。由于某种原因,透明度效果不佳。我使用了我自己在 Photoshop 中创建的带有或不带有透明度的新鲜 PNG 测试图像,一切都如广告所示。
当然,您在循环内所做的事情是根据您的需要进行修改的。
注意字节顺序,它真的会把事情搞砸!
Here's a shot. Tested, no leaks. No crashes.
.h
.m
I setup the xib with two UIImageViews and one UIButton. The top UIImageView was pre-loaded with an image with Interface Builder. Touch the text button and the image is processed and set to the second UIImageView.
BTW, I had a little trouble with your icons copied right off your post. The transparency didn't work very well for some reason. I used fresh PNG test images of my own created in Photoshop with and without transparency and it all worked as advertised.
What you do inside the loop is to be modified per your needs, of course.
Watch endianess, it can really mess things up!
我最近在 iPhone 应用程序中相当广泛地使用了蒙版,没有发生崩溃。该链接中的代码似乎甚至没有使用遮罩,只是进行了剪辑;唯一提到面具的是他尝试过的其他东西。更有可能的是,他是从后台线程调用的,
UIGraphicsBeginImageContext
不是线程安全的。如果不确切知道您想要达到什么效果,就很难就如何做到这一点提供建议。蒙版当然可以工作,无论是单独使用(以获得某种丝网印刷效果)还是剪辑在更真实的图像上绘制的覆盖颜色。我可能会使用遮罩或路径来设置剪切,然后绘制模具图像(使用 kCBGlendModeNormal 或 kCBGlendModeCopy),然后在其上绘制适当的纯色使用kCGBlendModeColor。
I've used masks fairly extensively recently in an iPhone app with no crashes. The code in that link doesn't even seem to be using masks, just clipping; the only mention of masks was as something else he tried. More likely he was calling that from a background thread,
UIGraphicsBeginImageContext
isn't thread safe.Without knowing exactly what effect you're trying to get, it's hard to give advice on how to do it. A mask certainly could work, either alone (to get a sort of silkscreened effect) or to clip an overlay color drawn on a more realistic image. I'd probably use a mask or a path to set the clipping, then draw the die image (using
kCBGlendModeNormal
orkCBGlendModeCopy
), and then paint the appropriate solid color over it usingkCGBlendModeColor
.