/**
* 通过颜色生成纯颜色的图片
*/
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[RootViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
#import "RootViewController.h"
#import "UIImage+CreatImageWithColor.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// UIImage *image = [UIImage imageWithColor:[UIColor redColor] imageWidth:100 imageWithHeight:100];
UIImage *image = [UIImage imageWithColor:[UIColor yellowColor]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(100, 100, 200, 200);
[self.view addSubview:imageView];
}
@end
#import <UIKit/UIKit.h>
@interface UIImage (CreatImageWithColor)
/**
* 通过颜色生成该纯颜色的图片
*
* @param color 生成图片的颜色
*
* @return 返回图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color;
/**
* 通过颜色生成该纯颜色的图片
*
* @param color 生成图片的颜色
* @param width 生成图片的宽
* @param height 生成图片的高
*
* @return 返回图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height;
@end
#import "UIImage+CreatImageWithColor.h"
@implementation UIImage (CreatImageWithColor)
+ (UIImage *)imageWithColor:(UIColor *)color{
return [UIImage imageWithColor:color imageWidth:100 imageWithHeight:100];
}
+ (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height{
//开启基于位图的图形上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height), NO, 0.0);
// 设置画笔的颜色
[color set];
// 画矩形,并填充
UIRectFill(CGRectMake(0, 0, width, height));
//获取图片
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
return resultImage;
}
@end