一,效果图。

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

二,代码。

RootViewController.m

【代码笔记】iOS-通过颜色来生成一个纯色图片
- (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;
}
【代码笔记】iOS-通过颜色来生成一个纯色图片

 

 

 
 

一,效果图。

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

二,代码。

RootViewController.m

【代码笔记】iOS-通过颜色来生成一个纯色图片
- (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;
}
【代码笔记】iOS-通过颜色来生成一个纯色图片

 

 

相关文章:

  • 2022-12-23
  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
  • 2022-01-15
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
相关资源
相似解决方案