为什么 initWithPatternImage 会丢失 PNG 的 alpha 值
有人见过 [UIColor initWithPatternImage] 忽略 PNG 的 alpha 值的问题吗?如果是这样,最好的解决办法是什么?
我使用 png 作为按钮对象数组的背景层,它包含图像中每个像素的几个预设的 alpha 值,该值将用作纹理背景。它作为图案/纹理颜色加载良好,但它会将所有关键透明区域显示为不透明黑色。
获得正确的 alpha 值非常重要,这样按钮图像才能正确显示。按钮框架不包括背景的 Alpha 阴影,因为那不是按钮的“可点击”部分。此外,我的按钮对象的图像和背景图像也使用透明度,因此它确实需要在每个按钮后面直接有一个清晰的背景,以让正确的真实当前颜色设置通过(最低层 UIView 将其背景颜色设置为当前用户选择的颜色)。仅为包含此纹理的 UIView 图层设置单个 alpha 值也无法满足我的需求。
任何帮助将不胜感激。我当前的解决方法是使用 png 来使用多个 UIImageView 的完全成熟、重复编程的布局,而不是使用图案填充的单个 UIView。
这是一段代码,但将 UIImage 转换为 UIColor 用作图案/纹理颜色是相当标准的:
UIView *selectorView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,320)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"SelectorViewBackground.png"]];
selectorView.backgroundColor = background;
[mainView addSubview:selectorView]; // pattern background layer. Add UIButtons on top of this selectorView layer
[self addSubview:mainView]; // current user selected color set in mainView.
[selectorView release];
[background release];
Anyone ever seen the problem of [UIColor initWithPatternImage] ignoring the alpha values of the PNG? If so, what's the best fix?
I am using a png as a background layer to my array of button objects and it contains several pre-set alpha values per pixel in the image that is to be used as a texture background. It loads fine as a pattern/texture-color, but it comes up with all key transparent area as opaque black.
It is important that I get the right alpha values so that the button images shows correctly. The button frames do not include the alpha shadows from the background as that is not the "clickable" portion of the button. Additionally, my button object's images and background images also makes use of transparency, so it really needs to have a clear background directly behind each button to let the proper true current color settings come through (lowest layer UIView will have its background color set to the current user's selected color). Setting just the single alpha value for the UIView layer containing this texture does not work for my needs either.
Any help would be appreciated. My current workaround would be to use fully-blown, repeatedly-programmed layout of several UIImageView using the png, instead of a single UIView with the pattern fill.
Here is a snippet of code, but it's pretty standard for turning a UIImage into a UIColor for use as a pattern/texture color:
UIView *selectorView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,320)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"SelectorViewBackground.png"]];
selectorView.backgroundColor = background;
[mainView addSubview:selectorView]; // pattern background layer. Add UIButtons on top of this selectorView layer
[self addSubview:mainView]; // current user selected color set in mainView.
[selectorView release];
[background release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在 UIView 上设置具有一定透明度的背景时遇到了同样的问题,
这就是我解决它的方法:
I had the same problem with setting a background on a UIView with some transparancy,
this is how I solved it:
这可能与:
平铺背景图像:我可以吗使用 UIImageView 可以轻松做到这一点吗?
基本上,尝试设置:
This is probably related to:
Tiled Background Image: Can I do that easily with UIImageView?
Basically, try setting:
不,我从来没有遇到过这个问题。我一直在应用程序中使用像上面这样的代码(尽管我经常将它与图层而不是视图结合使用,但这不应该对识别透明度产生影响)并且透明度始终工作得很好。
我会看看你的 PNG 文件。我注意到 iOS 有时对某些 PNG 选项/类型很挑剔(例如具有 8 位透明度的 8 位 PNG)。确保您的 PNG 保存为 24 位,透明度为 8 位(总共 32 位)。
另外,这是一个愚蠢的问题,但是您是否验证过 PNG 后面的视图/图层层次结构中没有任何黑色内容?有时候就是这样愚蠢的事情
No I've never had an issue with this. I use code like the above all the time for apps (though often I use it in conjunction with a layer instead of a view but that shouldn't make a difference with transparency being recognized) and always have had transparency work fine.
I'd look into your PNG file. I've noticed iOS sometimes being finicky with certain PNG options/types (like an 8 bit PNG with 8 bit transparency). Make sure your PNG is saved as 24 bit with 8 bit transparency (32 bit total).
Also, stupid question, but have you verified there isn't anything black in the view/layer hierarchy behind your PNG? Sometimes it's the stupid things like that
对于那些可能需要解决方法代码的人,其中背景模式可以布置为 UIScrollView 中的行,这里是(经过调整以消除机密性问题,如果在调用之前正确设置变量,则应该可以工作)。
请注意,应该有一些方法可以多次重复使用 UIImageView 的一个分配实例,以节省内存或加载时间,但上市时间是我目前的第一要务。享受旅程:)
For those who might need the work-around code where the background patterns can be laid out as rows in a UIScrollView, here it is (adjusted to remove confidentiality concerns, should work if variables properly set prior to call).
Note that there should be ways to reuse just the one allocated instance of UIImageView multiple times to either save memory or load times but time-to-market is my No. 1 driver right now. Enjoy the journey :)