iOS 中由颜色值生成图片

来源:互联网 发布:淘宝电瓶广告车 编辑:程序博客网 时间:2024/05/22 00:07

写一个UIImage的类扩展
.h

#import <UIKit/UIKit.h>@interface UIImage (BLHImage)+ (UIImage *)blh_imageWithColor:(UIColor *)color;@end

.m

#import "UIImage+BLHImage.h"@implementation UIImage (BLHImage)+ (UIImage *)blh_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

调用的时候

[self.confirmButton setBackgroundImage:[UIImage blh_imageWithColor:UIColorFromRGBOne(200)] forState:(UIControlStateDisabled)];
0 0
原创粉丝点击