相关疑难解决方法(0)

从UIColor创建UIImage以用作UIButton的背景图像

我正在创建一个这样的彩色图像:

CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
                                   [[UIColor redColor] CGColor]);
//  [[UIColor colorWithRed:222./255 green:227./255 blue: 229./255 alpha:1] CGColor]) ;
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

然后将其用作按钮的背景图像:

[resultButton setBackgroundImage:img forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

使用redColor可以很好地工作,但是我想使用RGB颜色(如注释行所示).当我使用RGB颜色时,按钮背景不会改变.

我错过了什么?

objective-c uibutton uiimage ios

154
推荐指数
5
解决办法
10万
查看次数

标签 统计

ios ×1

objective-c ×1

uibutton ×1

uiimage ×1