CoreGraphics Gradient未在iOS4上显示

时间:2012-03-28 14:59:56

标签: ios core-graphics

我有以下代码在iOS5上正常工作。

 //// Abstracted Graphic Attributes
NSString* textContent = [NSString stringWithFormat:@"%i",[myAnnotation.annotations count]];

//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

//// Gradient Declarations
NSArray* gradient3Colors = [NSArray arrayWithObjects: 
                            (id)[UIColor lightGrayColor].CGColor, 
                            (id)[UIColor darkGrayColor].CGColor, nil];
CGFloat gradient3Locations[] = {0, 1};
CGGradientRef gradient3 = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradient3Colors, gradient3Locations);


//// Oval Drawing
UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(0.5, 0.5, 34, 34)];
CGContextSaveGState(context);
[ovalPath addClip];
CGContextDrawRadialGradient(context, gradient3,
                            CGPointMake(17.5, 17.5), 10,
                            CGPointMake(17.5, 17.5), 30,
                            kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);

[[UIColor blackColor] setStroke];
ovalPath.lineWidth = 0.5;
[ovalPath stroke];


//// Text Drawing
CGRect textFrame = CGRectMake(0, 7.0, 35, 20);
[[UIColor whiteColor] setFill];
[textContent drawInRect: textFrame withFont: [UIFont fontWithName: @"HelveticaNeue" size: [UIFont systemFontSize]] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentCenter];

//// Cleanup
CGGradientRelease(gradient3);
CGColorSpaceRelease(colorSpace);   

这画一个带有灰色渐变的圆圈,中间是一个数字。在iOS5上,这显示正确。在iOS4上它没有错误或警告我使用错误的API和一切,但灰色渐变被绘制!有什么想法吗?

1 个答案:

答案 0 :(得分:2)

好的,iOS4出于某种原因不喜欢定义的颜色。

使用以下修复程序。

UIColor* myLightGrey = [UIColor colorWithRed: 0.66 green: 0.66 blue: 0.66 alpha: 1];
UIColor* myDarkGrey = [UIColor colorWithRed: 0.33 green: 0.33 blue: 0.33 alpha: 1];

//// Gradient Declarations
NSArray* gradient3Colors = [NSArray arrayWithObjects: 
                            (id)myLightGrey.CGColor, 
                            (id)myDarkGrey.CGColor, nil];
CGFloat gradient3Locations[] = {0, 1};