【问题标题】:CoreGraphics Gradient not shown on iOS4iOS4 上未显示 CoreGraphics 渐变
【发布时间】:2012-03-28 14:59:55
【问题描述】:

我有以下代码在 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,除了绘制灰色渐变之外的所有内容!有什么想法吗?

【问题讨论】:

    标签: ios core-graphics


    【解决方案1】:

    好吧,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};
    

    【讨论】:

    • 我猜这是 ARC-on-ios4 的问题,如果您在创建数组之前将标准颜色分配给变量,它也可以工作。
    • 我也看到了这个问题......黑色和灰色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多