【发布时间】:2012-06-11 22:18:09
【问题描述】:
我一直在尝试在我的 iOS 应用中为 UI 创建自己的渐变。我第一次使用 CAGradientLayer,但我对“阶梯式”外观感到失望,所以我一直在尝试 CGGradient。
- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGGradientRef skyGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 3;
CGFloat locations[3] = { 0.0, .5, 1.0 };
CGFloat components[12] = { .106, .73, .93333, 1.,
0.0, 0.0 , 1.0, 1.,
.106, .73, .93333, 1. };
rgbColorspace = CGColorSpaceCreateDeviceRGB();
skyGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGRect currentBounds = self.bounds;
CGPoint topLeft = CGPointMake(0.0f, 0.0f);
CGPoint bottomRight = CGPointMake(CGRectGetMaxX(currentBounds), CGRectGetMaxY(currentBounds));
CGContextDrawLinearGradient(currentContext, skyGradient, topLeft, bottomRight, 0);
CGGradientRelease(skyGradient);
CGColorSpaceRelease(rgbColorspace);
}
我在正确显示渐变时遇到问题。我有三台 iPad——每一代人一台。渐变在 iPad 3 上看起来不错,但在 iPad 1 或 2 上却不行。这真的很奇怪。我打算截屏并发布两个差异,但更奇怪的是,屏幕截图看起来相同(是的,两者的亮度相同)。
旧 iPad 上的颜色似乎真的褪色了。我知道 iPad 3 是视网膜显示器,但我认为它肯定不止于此。
【问题讨论】:
-
你确定这是代码,而不仅仅是屏幕?如果您在 Mac 上的图像编辑器中渲染相同的渐变并在两台设备上并排显示完全相同的图像会发生什么?
-
好主意,我试试看。
-
@DavidRönnqvist 我按照你的建议做了,并使用了来自 Illustrator 的具有完全相同渐变设置的图像。和以前一样,iPad 3 的颜色看起来与我的 Cinema Display 相同(或相似)。其他两台 iPad 和我的 iPhone 4 还差得远。所以我有点担心如何在多个设备上实现外观一致的 UI。有什么想法吗?
-
您不必担心。用户无法根据当前照明条件校准他们的设备,因此您永远无法保证所有情况下的颜色准确。只要您的应用相对于同一设备上的其他应用看起来相同,就应该没问题
-
谢谢大卫。所以现在只是想知道如何处理这个问题。因为你的回复一直是cmets的形式,所以我不能接受他们作为答案。
标签: ios ipad gradient quartz-graphics