开发者

How do I comfortably use CGColor in an NSArray with CGGradient

开发者 https://www.devze.com 2023-03-05 00:05 出处:网络
I have two UIColor instances and want to use them creating a gradient. The code works, but it gives me a warning when I call the arrayWithObject: constructor:

I have two UIColor instances and want to use them creating a gradient. The code works, but it gives me a warning when I call the arrayWithObject: constructor:

warning: Semantic Issue: Incompatible pointer types sending 'CGColorRef' (aka 'struct CGColor *') to parameter of type 'id'

I suspect there lurks other issues related to the warning (leaks for instance). Here is the snippet:

   UIColor *startColor, *endColor; 
   // ...
   NSArray *colors = [NSArray arrayWithObjects:
                      startColor.CGColor, endColor.CGColor, nil];
   CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, 
                     (CFArrayRef) colors, NULL);

How can I rewrite the code to get rid of this warning in a safe way ?

  • I tried creating a CGArrayRef manually, but that got a bit nasty after a whi开发者_如何学编程le.
  • CGGradientCreateWithColorComponents works, but I don't like having to manually merge all color components into an CGFloat components[]


Try this

UIColor *startColor, *endColor; 
   // ...
   NSArray *colors = [NSArray arrayWithObjects:
                      (id)startColor.CGColor, (id)endColor.CGColor, nil];
   CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, 
                     (CFArrayRef) colors, NULL);
0

精彩评论

暂无评论...
验证码 换一张
取 消