UIImage+PYJColorBecomeImage.h:
// // UIImage+PYJColorBecomeImage.h // 颜色转成图片 // // Created by PengYunjing on 16/8/24. // Copyright © 2016年 PYJ. All rights reserved. // #import <UIKit/UIKit.h> @interface UIImage (PYJColorBecomeImage) + (UIImage*)imageWithColor:(UIColor*)color; @end
UIImage+PYJColorBecomeImage.m:
// // UIImage+PYJColorBecomeImage.m // 颜色转成图片 // // Created by PengYunjing on 16/8/24. // Copyright © 2016年 PYJ. All rights reserved. // #import "UIImage+PYJColorBecomeImage.h" @implementation UIImage (PYJColorBecomeImage) + (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 *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; } @end