简体   繁体   English

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

[英]iOS 5.1 : -[UIColor colorWithPatternImage:] background color draws solid black

While testing my application in today's release of iOS 5.1 GM, I noticed that some of my views are drawing solid black rather than their patterned background color. 在今天发布的iOS 5.1 GM中测试我的应用程序时,我注意到我的一些视图是绘制纯黑色而不是图案背景颜色。 The exact same code works fine on previous iOS releases (tested on 4.2 - 5.0.1). 完全相同的代码在以前的iOS版本上运行良好(在4.2 - 5.0.1上测试)。

See screenshots: 查看截图: 问题截图

Has anybody else experienced this? 有没有人经历过这个? Is there a workaround? 有解决方法吗?

Answering my own question (it took me a few days to debug this, so hopefully this saves somebody else some time ;) ): 回答我自己的问题(我花了几天的时间来调试这个问题,所以希望这能节省一些时间;)):

The root cause involves using an patterned UIColor (via +[UIColor colorWithPatternImage:] ) as a background color on a UIView that is above a UIImageView with the same image. 根本原因涉及使用图案化的UIColor(通过+[UIColor colorWithPatternImage:] )作为UIView上的背景颜色,该UIView位于具有相同图像的UIImageView上方。

Example: 例:

    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];

Both views draw black, and there appears to be a caching issue where all other uses of the image draws black until the application is suspended/resumed. 两个视图都绘制为黑色,并且似乎存在缓存问题,其中图像的所有其他用途都将呈现黑色,直到应用程序暂停/恢复为止。

I filed issue #10795514 with Apple to report this, but it looks like it made it into 5.1. 我向Apple提交了问题#10795514来报告这个问题,但看起来它已经变成了5.1。 A reduction of this problem is available at: http://iccir.com/public/radar/Radar10795514.zip 有关此问题的减少,请访问: http//iccir.com/public/radar/Radar10795514.zip

The only workaround I found was to flatten the view hierarchy and draw the pattern image twice in the same view. 我发现的唯一解决方法是展平视图层次结构并在同一视图中绘制两次图案图像。

I was having this problem with iOS 5.1 on an iPad where I was using colorWithPatternImage on a UIScrollView like this: 我在iPad上遇到iOS 5.1的这个问题,我在UIScrollView上使用colorWithPatternImage,如下所示:

scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YOURIMAGE.jpg"]];

This works in iOS 6 (eg iPad2 and beyond), however, on an original iPad, where you can only update your iOS to 5.1.1, this will show up white or another solid color you defined somewhere. 这适用于iOS 6(例如iPad2及更高版本),但是,在原始iPad上,您只能将iOS更新为5.1.1,这将显示您在某处定义的白色或其他纯色。 The fix is to use a slightly less attractive method where you set the backgroundView of the scrollView like this: 修复是使用一个稍微不那么有吸引力的方法,你可以像这样设置scrollView的backgroundView:

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

I've tested this in iOS 6 and iOS 5.1, so it should also apply to the iPhone if you're running into problems there. 我已经在iOS 6和iOS 5.1中对此进行了测试,因此如果您遇到问题,它也应该适用于iPhone。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM