Monday 20 April 2015

Set UIImage by setting color name

#define UIColorFromRGBHex(rgbValue) \
  [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
                  green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
                   blue:((float)(rgbValue & 0xFF))/255.0 \
                  alpha:1.0]


#define UIColorFromRGB(nRed,nGreen,nBlue) \
  [UIColor colorWithRed:(nRed)/255.0 \
                  green:(nGreen)/255.0 \
                   blue:(nBlue)/255.0 \
                  alpha:1.0]

@interface UIImage (imageWithColor)

+ (UIImage *)imageWithColor:(UIColor *)color;

@end


@implementation UIImage (imageWithColor)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end


[imageView setImage:[UIImage imageWithColor:UIColorFromRGB(255,0,0)]];

No comments:

Post a Comment