Organizational Research By

Surprising Reserch Topic

creating a uiimage from a uicolor to use as a background image for uibutton


creating a uiimage from a uicolor to use as a background image for uibutton  using -'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];


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.

What,am,I,missing?'


    This question already has an answer here:
    
        
            How to create a colored 1x1 UIImage on the iPhone dynamically?
                
                    4 answers
                
        
    

asked Oct 13, 2015 by tushar2k6
0 votes
7 views



Related Hot Questions

4 Answers

0 votes

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.

answered Oct 13, 2015 by deepak gupta
0 votes

Are you using ARC? The problem here is that you don't have a reference to the UIColor object that you're trying to apply; the CGColor is backed by that UIColor, but ARC will deallocate the UIColor before you have a chance to use the CGColor.

The clearest solution is to have a local variable pointing to your UIColor with the objc_precise_lifetime attribute.

You can read more about this exact case on this article about UIColor and short ARC lifetimes or get more details about the objc_precise_lifetime attribute.

answered Oct 13, 2015 by ashish singh
0 votes

Add the dots to all values:

[[UIColor colorWithRed:222./255. green:227./255. blue: 229./255. alpha:1] CGColor]) ;

Otherwise, you are dividing float by int.

answered Oct 13, 2015 by sumit_jaiswalmca
0 votes

Xamarin.iOS solution

 public UIImage CreateImageFromColor()
        {
            var imageSize = new SizeF(30, 30);
            var imageSizeRectF = new RectangleF(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;
        }
answered Oct 13, 2015 by nikhilapatil

...