您的位置:首页 > 移动开发 > IOS开发

【代码笔记】iOS-通过颜色来生成一个纯色图片

2016-06-12 09:17 477 查看
一,效果图。



二,代码。

RootViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
imageView.backgroundColor=[UIColor yellowColor];
imageView.image=[self buttonImageFromColor:[UIColor redColor]];
[self.view addSubview:imageView];

}
//通过颜色来生成一个纯色图片
- (UIImage *)buttonImageFromColor:(UIColor *)color{

CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: