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

Tho*_*ten 154 objective-c uibutton uiimage ios

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

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颜色时,按钮背景不会改变.

我错过了什么?

Sco*_*man 139

我在UIButton周围创建了一个类别,以便能够设置按钮的背景颜色并设置状态.你可能会觉得这很有用.

@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;
}
Run Code Online (Sandbox Code Playgroud)

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

Swift 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
  }
}
Run Code Online (Sandbox Code Playgroud)

Swift 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!
    }
}
Run Code Online (Sandbox Code Playgroud)

用于

let img = UIImage.from(color: .black)
Run Code Online (Sandbox Code Playgroud)

  • 虽然现在在 Swift 中我们使用的是扩展而不是类别,而不是 UIButton 上的类别,但您不认为最好在 UIImage 上为初始值设定项而不是 UIButton 进行扩展吗? (2认同)

ben*_*ben 12

Xamarin.iOS解决方案

 public UIImage CreateImageFromColor()
 {
     var imageSize = new CGSize(30, 30);
     var imageSizeRectF = new CGRect(0, 0, 30, 30);
     UIGraphics.BeginImageContextWithOptions(imageSize, false, 0);
     var context = UIGraphics.GetCurrentContext();
     var red = new CGColor(255, 0, 0);
     context.SetFillColor(red);
     context.FillRect(imageSizeRectF);
     var image = UIGraphics.GetImageFromCurrentImageContext();
     UIGraphics.EndImageContext();
     return image;
 }
Run Code Online (Sandbox Code Playgroud)


Jes*_*sak 6

你在用ARC吗?这里的问题是你没有引用UIColor你想要应用的对象; 由此CGColor支持UIColor,但ARC会UIColor在您有机会使用之前解除分配CGColor.

最明确的解决方案是UIColor使用objc_precise_lifetime属性指向您的局部变量.

您可以在本文中详细了解有关UIColor和短ARC生命周期的详细信息,或获取有关objc_precise_lifetime属性的更多详细信息.


小智 5

将点添加到所有值:

[[UIColor colorWithRed:222./255. green:227./255. blue: 229./255. alpha:1] CGColor]) ;
Run Code Online (Sandbox Code Playgroud)

否则,您会将float除以int。

  • 不正确-整数参数会在对表达式求值之前自动提升为浮点值(在这种情况下为double),因为左操作数的类型为double。[请参阅此答案以获取详细说明](http://stackoverflow.com/questions/5563000/implicit-type-conversion-rules-in-c-operators) (5认同)

akn*_*new 4

我认为 227./255 中的 255 被视为整数并且除法总是返回 0