2012-04-16 23 views
7

如何设置UIColor值成CGContextSetRGBStrokeColor如何设置的UIColor值要CGContextSetRGBStrokeColor

//这是红色,我设定为CGContextSetRGBStrokeColor CGContextSetRGBStrokeColor(contextRef,1,0,0,1.00f);

现在我确实有UIColor,它具有RGB值,我需要将此值设置为CGContextSetRGBStrokeColor

任何一个建议我如何设置UIColor值的CGContextSetRGBStrokeColor

回答

9
CGFloat red, green, blue, alpha; 
[color getRed:&red green:&green blue:&blue alpha:&alpha]; 
CGContextSetRGBStrokeColor(contextRef, red, green, blue, alpha); 
+0

这是为什么downvoted?这是问题的答案。 – shapecatcher 2012-05-18 15:07:00

13

备选:

CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]); 

// or 
[[UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f] CGColor]; 
1

对于懒:

let myUIColor = UIColor.purpleColor() 
var r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat = 0 
myUIColor.getRed(&r, green: &g, blue: &b, alpha: &a) 
CGContextSetRGBStrokeColor(c, r, g, b, a) 

// or 
CGContextSetStrokeColorWithColor(myUIColor.CGColor); 

#SwiftStyle

+0

使用'CGContextSetStrokeColorWithColor(myUIColor.CGColor)'时不需要设置上下文吗? – Crashalot 2016-01-18 07:18:18

0

在SWIFT 3.0

let context = UIGraphicsGetCurrentContext() 
context!.setStrokeColor(red: 1,green: 1,blue: 1,alpha: 1) 

或者你可以SER颜色如下

context!.setStrokeColor(UIColor.red.cgColor)//or any UIColor