colorWithPatternImage在iphone 4中创建黑色网格

时间:2011-10-02 05:05:24

标签: uicolor iphone-4

我有

UIView *topPart = [[UIView alloc] initWithFrame:CGRectMake(9, 0, 302, 318)];
topPart.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern.png"]];
[someScroller addSubview:topPart];
[topPart release];

当我使用3gs或iphone模拟器时,我可以看到模式很好,但是当我在实际的iPhone 4上测试时,我看到了模式图像,但是每个模式都有黑色线条,所以它看起来像一个网格。我检查了图案图像,没有黑色边框或任何东西。

有人有不同的问题,但它可能是相同的解决方案 colorWithPatternImage with iPhone 4 Retina Display (image@2x.png)

但他们建议使用 - (void)drawRect:方法,唯一的问题是我不知道该怎么做。

由于

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,发现这是因为我的png是灰度(不是RGB)

要检查是否是这种情况,只需在Xcode中选择图像,显示Utilities侧边栏(右侧显示的侧边栏)并查看色彩空间。

要解决此问题,您可以使用自己喜欢的图像编辑器,但对于photoshop,请执行以下操作: 打开灰度图像 全选并复制 创建一个新文档(但这次确保颜色空间是RGB) 糊

希望有所帮助:)

答案 1 :(得分:1)

我通过结合这两个帖子的答案来解决这个问题 https://devforums.apple.com/message/259150#259150

How can I use CGContextDrawTiledImage to tile an image?

UIImage *thePattern= [self GetFineTiles:[UIImage imageNamed:@"Feedbackpattern.png"]];
theView.backgroundColor = [UIColor colorWithPatternImage:thePattern];

-(UIImage*)GetFineTiles:(UIImage*)badImage{
    UIGraphicsBeginImageContext(badImage.size);
    CGContextRef imageContext = UIGraphicsGetCurrentContext();
    CGContextDrawTiledImage(imageContext, (CGRect){CGPointZero,badImage.size}, badImage.CGImage);
    UIImage *goodPattern = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return goodPattern;
}

希望这有助于其他人

相关问题