2012-09-12 116 views
0

我有以下代码。UIColor colorWithPatternImage更改图像的颜色

//Create a view to place repeated arrow in. 
UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)]; 

//Add Repeated Arrows to the top of the view using a color pattern. 
UIImage *arrowImage = [UIImage imageNamed:@"arrow-down"]; 
UIColor *repeatedArrowColor = [UIColor colorWithPatternImage:arrowImage]; 
CGRect repeatedArrowFrame = CGRectMake(0, 0, 320.0, [arrowImage size].height); 
UIView *repeatedArrowView = [[UIView alloc] initWithFrame:repeatedArrowFrame]; 
[repeatedArrowView setBackgroundColor:repeatedArrowColor]; 

[rootView addSubview:repeatedArrowView]; 

正在使用的图像是带有颜色箭头的png#323232。我通过右键单击.app文件并选择显示包装内容来验证这一点。

Image used to generate pattern.

然而,当图案的iPhone模拟器屏幕上显示的箭头的颜色是#262626或#252525。

Wrong colored arrows in the iOS simulator

有没有人遇到这个问题?如果是这样,你是如何解决问题的,或者你是否解决了这个问题?

回答

0

总之,每个人都有这些问题。如果您在预览中打开该图像并使用小精灵(开发工具),则会看到像素的值与文件中的确切值不同。这个值与你在模拟器中看到的有什么不同?

你的PNG是否有嵌入其中的颜色配置文件?如果删除它可能会有帮助(pngcrush将作为选项执行此操作)。如果没有,或者不解决它,那么你可能需要使用第一加载图像作为CGImageRef:

CGImageRef CGImageCreateWithPNGDataProvider (
    CGDataProviderRef source, 
    const CGFloat decode[], 
    bool shouldInterpolate, 
    CGColorRenderingIntent intent 
); 

,并用颜色意图(第一)鬼混,并作为最后的选择解码阵列。

+0

谢谢你的回答。我从来没有遇到过这个问题。可能是因为我已经离开了Apple的Compress PNGs选项,或者在使用png之前应用了ImageOptim。 – Tobias

+0

只是好奇 - 你是如何得到你想要的价值的? –

+0

那么,在你回复之前,我已经停止使用png了。相反,我使用CoreGraphics绘制单个箭头,然后将其用作模式的基础。 – Tobias