<bdo id='SkVqC'></bdo><ul id='SkVqC'></ul>

<legend id='SkVqC'><style id='SkVqC'><dir id='SkVqC'><q id='SkVqC'></q></dir></style></legend>

  • <i id='SkVqC'><tr id='SkVqC'><dt id='SkVqC'><q id='SkVqC'><span id='SkVqC'><b id='SkVqC'><form id='SkVqC'><ins id='SkVqC'></ins><ul id='SkVqC'></ul><sub id='SkVqC'></sub></form><legend id='SkVqC'></legend><bdo id='SkVqC'><pre id='SkVqC'><center id='SkVqC'></center></pre></bdo></b><th id='SkVqC'></th></span></q></dt></tr></i><div id='SkVqC'><tfoot id='SkVqC'></tfoot><dl id='SkVqC'><fieldset id='SkVqC'></fieldset></dl></div>

      1. <small id='SkVqC'></small><noframes id='SkVqC'>

        <tfoot id='SkVqC'></tfoot>

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

        时间:2023-07-09

      2. <small id='pDQbS'></small><noframes id='pDQbS'>

          • <bdo id='pDQbS'></bdo><ul id='pDQbS'></ul>

                <i id='pDQbS'><tr id='pDQbS'><dt id='pDQbS'><q id='pDQbS'><span id='pDQbS'><b id='pDQbS'><form id='pDQbS'><ins id='pDQbS'></ins><ul id='pDQbS'></ul><sub id='pDQbS'></sub></form><legend id='pDQbS'></legend><bdo id='pDQbS'><pre id='pDQbS'><center id='pDQbS'></center></pre></bdo></b><th id='pDQbS'></th></span></q></dt></tr></i><div id='pDQbS'><tfoot id='pDQbS'></tfoot><dl id='pDQbS'><fieldset id='pDQbS'></fieldset></dl></div>
              1. <tfoot id='pDQbS'></tfoot><legend id='pDQbS'><style id='pDQbS'><dir id='pDQbS'><q id='pDQbS'></q></dir></style></legend>
                  <tbody id='pDQbS'></tbody>
                  本文介绍了从 UIColor 创建 UIImage 以用作 UIButton 的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I'm creating a colored image like this:

                  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();
                  

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

                  and then use it as the background image of a button:

                  [resultButton setBackgroundImage:img forState:UIControlStateNormal];
                  

                  使用 redColor 效果很好,但是我想使用 RGB 颜色(如注释行所示).当我使用 RGB 颜色时,按钮背景没有改变.

                  This works great using the redColor, however I want to use an RGB color (as shown in the commented line). When I use the RGB color, the buttons background isn't changed.

                  我错过了什么?

                  推荐答案

                  我围绕 UIButton 创建了一个类,可以设置按钮的背景颜色和设置状态.您可能会发现这很有用.

                  I created a category around UIButton to be able to set the background color of the button and set the state. You might find this useful.

                  @implementation  UIButton (ButtonMagic)
                  
                  - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
                      [self setBackgroundImage:[UIButton imageFromColor:backgroundColor] forState:state];
                  }
                  
                  + (UIImage *)imageFromColor:(UIColor *)color {
                      CGRect rect = CGRectMake(0, 0, 1, 1);
                      UIGraphicsBeginImageContext(rect.size);
                      CGContextRef context = UIGraphicsGetCurrentContext();
                      CGContextSetFillColorWithColor(context, [color CGColor]);
                      CGContextFillRect(context, rect);
                      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
                      UIGraphicsEndImageContext();
                      return image;
                  }
                  

                  这将是我本月开源的一组帮助类别的一部分.

                  This will be part of a set of helper categories I'm open sourcing this month.

                  斯威夫特 2.2

                  extension UIImage {
                  static func fromColor(color: UIColor) -> UIImage {
                      let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
                      UIGraphicsBeginImageContext(rect.size)
                      let context = UIGraphicsGetCurrentContext()
                      CGContextSetFillColorWithColor(context, color.CGColor)
                      CGContextFillRect(context, rect)
                      let img = UIGraphicsGetImageFromCurrentImageContext()
                      UIGraphicsEndImageContext()
                      return img
                    }
                  }
                  

                  斯威夫特 3.0

                  extension UIImage {
                      static func from(color: UIColor) -> UIImage {
                          let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
                          UIGraphicsBeginImageContext(rect.size)
                          let context = UIGraphicsGetCurrentContext()
                          context!.setFillColor(color.cgColor)
                          context!.fill(rect)
                          let img = UIGraphicsGetImageFromCurrentImageContext()
                          UIGraphicsEndImageContext()
                          return img!
                      }
                  }
                  

                  用作

                  let img = UIImage.from(color: .black)
                  

                  这篇关于从 UIColor 创建 UIImage 以用作 UIButton 的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何检测表格视图单元格中的一个按钮 下一篇:UIButton:为选中状态设置图片

                  相关文章

                    1. <i id='7OS8f'><tr id='7OS8f'><dt id='7OS8f'><q id='7OS8f'><span id='7OS8f'><b id='7OS8f'><form id='7OS8f'><ins id='7OS8f'></ins><ul id='7OS8f'></ul><sub id='7OS8f'></sub></form><legend id='7OS8f'></legend><bdo id='7OS8f'><pre id='7OS8f'><center id='7OS8f'></center></pre></bdo></b><th id='7OS8f'></th></span></q></dt></tr></i><div id='7OS8f'><tfoot id='7OS8f'></tfoot><dl id='7OS8f'><fieldset id='7OS8f'></fieldset></dl></div>
                      <tfoot id='7OS8f'></tfoot>
                      • <bdo id='7OS8f'></bdo><ul id='7OS8f'></ul>
                    2. <small id='7OS8f'></small><noframes id='7OS8f'>

                      <legend id='7OS8f'><style id='7OS8f'><dir id='7OS8f'><q id='7OS8f'></q></dir></style></legend>