ios - iOS4 上未显示 CoreGraphics 渐变

标签 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 和除灰色渐变之外的所有内容!有什么想法吗?

最佳答案

好吧,出于某种原因,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};

关于ios - iOS4 上未显示 CoreGraphics 渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9910269/

相关文章:

ios - iOS 嵌套数组的 objectAtIndex

objective-c - 检测是否有任何窗口被拖动

ios - 返回 UIViewController 时 UILabel 不更新

ios - 翻转和旋转

objective-c - opencv 构建问题 ("_cvLoadImage",引用自)

ios - iOS-CAShapeLayer.strokeEnd似乎不起作用

macos - 在所见即所得 Mac 应用程序中布置用于打印的图像的最佳实践?

objective-c - iOS:创建一个较暗版本的 UIImage 并保持透明像素不变?

iphone - iOS Quartz 在 pdf 中嵌入字体

ios - ARC语义问题,语义问题错误。