4

有人见过 [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];
4

4 回答 4

3

我在具有透明度的 UIView 上设置背景时遇到了同样的问题,这就是我解决它的方法:

theView.backgroundColor = [UIColor clearColor];
theView.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"the_image_with_transparancy.png"]].CGColor;
于 2011-07-07T12:26:49.340 回答
1

这可能与:

平铺背景图像:我可以使用 UIImageView 轻松做到这一点吗?

基本上,尝试设置:

[view setOpaque:NO];
[[view layer] setOpaque:NO];
于 2011-05-05T01:03:02.187 回答
0

不,我从来没有遇到过这个问题。我一直在为应用程序使用类似上面的代码(尽管我经常将它与图层而不是视图结合使用,但这不应该对识别透明度产生影响)并且总是让透明度工作正常。

我会查看您的 PNG 文件。我注意到 iOS 有时对某些 PNG 选项/类型(例如 8 位透明度的 8 位 PNG)很挑剔。确保您的 PNG 保存为 24 位,透明度为 8 位(总共 32 位)。

另外,愚蠢的问题,但是您是否验证过PNG后面的视图/图层层次结构中没有任何黑色?有时是这样的愚蠢的事情

于 2011-02-23T04:30:06.317 回答
0

对于那些可能需要在 UIScrollView 中将背景模式布置为行的变通代码的人,这里是(调整以消除机密性问题,如果在调用之前正确设置变量应该可以工作)。

请注意,应该有办法多次重用分配的 UIImageView 的一个实例,以节省内存或加载时间,但上市时间是我现在的第一驱动力。旅行愉快 :)

    UIImageView *selectorView;
    for (int i = 0; i < numRows; ++i) {
        selectorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectorViewBackground.png"]];
        selectorView.frame = CGRectMake(0, i * patternHeight, patternWidth, patternHeight);
        [mainView addSubview:selectorView];
        [selectorView release];         
    }
于 2011-02-24T04:28:19.643 回答