UIColor 直接生成 UIImage 和 圆角纯色UIImage

来源:互联网 发布:刷积分软件 编辑:程序博客网 时间:2024/06/04 17:50

直接使用UIColor生成所需要才纯色UIImage

/** * 将UIColor变换为UIImage * **/+ (UIImage *)createImageWithColor:(UIColor *)color{    //设置长宽    CGRect rect = CGRectMake(0.0f, 0.0f, 5.0f, 5.0f);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return resultImage;}

生成圆角纯色UIImage的方法

//生成圆角UIIamge 的方法- (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius{    UIImage *original = self;    CGRect frame = CGRectMake(0, 0, original.size.width, original.size.height);    // 开始一个Image的上下文    UIGraphicsBeginImageContextWithOptions(original.size, NO, 1.0);    // 添加圆角    [[UIBezierPath bezierPathWithRoundedRect:frame                                cornerRadius:cornerRadius] addClip];    // 绘制图片    [original drawInRect:frame];    // 接受绘制成功的图片    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}
0 0
原创粉丝点击