UIView中只有白色填充颜色是透明的 [英] Only White Fill Color is Transparent in UIView

查看:88
本文介绍了UIView中只有白色填充颜色是透明的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView 设置为 opaque = NO ,并且一切正常。在 drawRect 中,我正在进行自定义绘制,并且可以正常工作

I've got a UIView that is set to opaque = NO and it all works nicely. In the drawRect I'm doing custom drawing, and this works

CGContextSetFillColor(context, CGColorGetComponents([UIColor blueColor].CGColor)); 
CGContextFillRect(context, labelOutside);
CGContextAddRect(context, labelOutside);

但这是

CGContextSetFillColor(context, CGColorGetComponents([UIColor whiteColor].CGColor)); 
CGContextFillRect(context, labelOutside);
CGContextAddRect(context, labelOutside);

结果是未生成填充(您甚至可以看到我通过CGContext绘制的其他内容)。如何绘制白色填充?

results in NO fill being produced (you can even see other stuff that I drew on the CGContext through it). How can I draw a white fill?

注意:如果我将控件设置为不透明,则仍然无法使用。

Note: If I set the control not be opaque, it still doesn't work.

推荐答案

为什么不使用 CGContextSetFillColorWithColor ?然后,您可以直接传递CGColor对象,而不是提取其成分并假定它与上下文位于相同的颜色空间中。

Why not use CGContextSetFillColorWithColor? Then you can pass the CGColor object directly instead of extracting its components and assuming it's in the same color space as the context.

编辑后添加:


这有效

this works

CGContextSetFillColor(context, CGColorGetComponents([UIColor blueColor].CGColor)); 

但这是

CGContextSetFillColor(context, CGColorGetComponents([UIColor whiteColor].CGColor)); 

结果是未生成填充(您甚至可以看到我通过CGContext绘制的其他内容)。

results in NO fill being produced (you can even see other stuff that I drew on the CGContext through it).

如上所述,这种设置填充颜色的方式假定该颜色与上下文位于相同的颜色空间中。

As I mentioned, this way of setting the fill color assumes that the color is in the same color space as the context.

通常,大多数CGContext在RGB颜色空间中。另一方面, whiteColor 几乎可以肯定是在白色空间中,只有一个或两个(白色和Alpha)分量。

Normally, most CGContexts are in an RGB color space. whiteColor, on the other hand, is almost certainly in a white color space, which has only one or two (white and alpha) components.

因此,由于上下文位于RGB颜色空间中,因此 CGContextSetFillColor 预计将传递三或四个(红色,绿色,蓝色和Alpha)组件。当您获得 whiteColor 的CGColor的组件时,您将获得一个指向仅包含两个组件的C数组的指针。 CGContextSetFillColor 会读取它想要的三个或四个组成部分,无论如何;如果您通过的次数较少,它可能会在它们之后找到垃圾,或者可能会找到零。

So, since the context is in an RGB color space, CGContextSetFillColor expects to be passed three or four (red, green, blue, and alpha) components. When you get the components of whiteColor's CGColor, you get a pointer to a C array holding only two components. CGContextSetFillColor will read the three or four components it wants no matter what; if you pass fewer, it may find garbage after them, or it may find zeroes.

如果它在alpha位置中找到零,那么结果是您拥有将填充颜色设置为黄色(红色= 1.0,绿色= 1.0,蓝色= 0.0)和白色(全部三个= 1.0)之间,且alpha值为零。这就是导致填充不显示的原因:您正在填充透明颜色。

If it finds a zero in the alpha position, then the result is that you have set the fill color to something between yellow (red=1.0, green=1.0, blue=0.0) and white (all three=1.0) with zero alpha. That's what causes the fill to not show up: You are filling with a transparent color.

问题是上下文所在的颜色空间之间不匹配(三个或三个四个分量)和颜色所在的色彩空间(带有一个或两个分量)。解决方案是消除不匹配,这就是 CGContextSetFillColorWithColor 的作用:由于传递了整个CGColor对象,而不仅仅是数字数组,因此Core Graphics将能够设置将上下文的颜色空间设置为颜色对象的颜色空间,并正确解释颜色对象的组成部分。

The problem is that mismatch between the color space the context is in (with three or four components) and the color space the color is in (with one or two components). The solution is to remove the mismatch, which is what CGContextSetFillColorWithColor does: Since you are passing the whole CGColor object, not just an array of numbers, Core Graphics will be able to set the context's color space to that of the color object and interpret the color object's components correctly.

这篇关于UIView中只有白色填充颜色是透明的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆