用指定颜色生成指定尺寸的图片
特别注意需要UIKit框架
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> //需要UIKit框架 @interface YFCreateImage : NSObject /**用指定颜色生成指定尺寸的图片*/ -(UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size; @end
#import "YFCreateImage.h" @implementation YFCreateImage /**用指定颜色生成指定尺寸的图片*/ -(UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size { CGRect rect = CGRectMake(0.0f,0.0f,size.width,size.height); UIGraphicsBeginImageContext(rect.size); CGContextRef context =UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *myImage =UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return myImage; } @end
也可以更换为下面方法使用
+(UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size;