2015/03/03

Get RBGA values of UIColor

I have been using next method to get RGBA values of UIColor :

-getRed:green:blue:alpha:

This has existed since iOS 5, I guess this method was not able to get values correctly for gray color, for example, [UIColor grayColor], So I had to see its return value (Bool) and I had to use another method if failed.
But I found this method worked for gray color in Swift. And I also tried it in Objective-C, then I found it succeeded. Oh it has changed...

//Objective-C
{
    UIColor *color = [UIColor grayColor];
    CGFloat red,green,blue,alpha;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    NSLog(@"%f,%f,%f,%f",red,green,blue,alpha);//0.500000,0.500000,0.500000,1.000000
}

//Swift
{
    var red:CGFloat = 0, green:CGFloat = 0, blue:CGFloat = 0, alpha:CGFloat = 0
    color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
}
Swift needed initialization.


memo : Swift 1.1
Develop | Comments(0) | Trackback(0)
Comment

管理者のみに表示