iOS 5.1: -[UIColor colorWithPatternImage:] 背景颜色绘制为纯黑色

8

在测试我的应用程序时,我发现在今天发布的iOS 5.1 GM中,我的一些视图绘制的是纯黑色而不是它们的图案背景颜色。在之前的iOS版本(已测试4.2-5.0.1)上,完全相同的代码可以正常工作。

请参见屏幕截图: 问题的屏幕截图

是否有其他人遇到过这个问题?是否有解决方法?

2个回答

12

回答自己的问题(我花了几天时间调试,希望这能节省其他人的时间;)):

根本原因涉及在UIView上使用具有模式的UIColor(通过+ [UIColor colorWithPatternImage:] )作为背景颜色,而该UIView位于具有相同图像的UIImageView之上。

示例:

    UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
    [_containerView addSubview:imageView];

    UIColor *patternColor = [UIColor colorWithPatternImage:anImage];
    UIView  *patternView = [[UIView alloc] initWithFrame:frame];
    [patternView setBackgroundColor:patternColor];
    [_containerView addSubview:patternView];

两个视图都是黑色的,似乎存在缓存问题,直到应用程序被暂停/恢复之前,所有其他使用该图像的地方都会绘制黑色。

我向苹果提交了#10795514问题报告,但看起来它已经进入了5.1版本。可以在此处找到此问题的简化版本:http://iccir.com/public/radar/Radar10795514.zip

我发现的唯一解决方法是将视图层次结构展平,并在同一个视图中绘制模式图像两次。


真正的问题是colorWithPatternImage在iOS5中不适用于UIImageView,显然应该使用UIView... - anders

0
我在 iPad 上遇到了一个 iOS 5.1 的问题,我正在使用像这样的 colorWithPatternImage 在 UIScrollView 上:
scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YOURIMAGE.jpg"]];

这在iOS 6中有效(例如iPad2及更高版本),但是在原始的iPad上,您只能将iOS更新到5.1.1,在这种情况下,它将显示为白色或其他您在某个地方定义的纯色。解决方法是使用稍微不太吸引人的方法,即像这样设置scrollView的backgroundView:

scrollView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YOURIMAGE.png"]];

我已在iOS 6和iOS 5.1中进行了测试,因此如果您在iPhone上遇到问题,它也应该适用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接