设为首页 加入收藏

TOP

【代码笔记】iOS-通过颜色来生成一个纯色图片
2017-10-13 10:23:41 】 浏览:5830
Tags:代码 笔记 iOS- 通过 颜色 生成 一个 图片

一,效果图。

二,代码。

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;
}
复制代码

 

 

 
 
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS 事件传递响应链 下一篇【改】iOS学习之NSAttributedStri..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目