zoukankan      html  css  js  c++  java
  • iOS颜色转换成图片的方法

     1 //  颜色转换为背景图片
     2 - (UIImage *)imageWithColor:(UIColor *)color {
     3     CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
     4     UIGraphicsBeginImageContext(rect.size);
     5     CGContextRef context = UIGraphicsGetCurrentContext();
     6     
     7     CGContextSetFillColorWithColor(context, [color CGColor]);
     8     CGContextFillRect(context, rect);
     9     
    10     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    11     UIGraphicsEndImageContext();
    12     
    13     return image;
    14 }

    使用例子

     1 - (void)viewDidLoad {
     2     [super viewDidLoad];
     3     
     4     UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)];
     5     [button1 setTitle:@"button1" forState:UIControlStateNormal];
     6     button1.backgroundColor = [UIColor orangeColor];
     7     [button1 addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
     8     [button1 addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
     9     [self.view addSubview:button1];
    10     
    11     UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(170, 200, 100, 50)];
    12     [button2 setTitle:@"button2" forState:UIControlStateNormal];
    13     [button2 setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateNormal];
    14     [button2 setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];
    15     [self.view addSubview:button2];
    16 }

    效果如下图:

  • 相关阅读:
    孤儿进程与僵尸进程
    python with as的用法
    工作目录与os.getcwd()
    内置模块
    迭代器,生成器
    表达式,语句
    字符流
    字节流
    File
    触发器的操作
  • 原文地址:https://www.cnblogs.com/wanli-leon/p/12148794.html
Copyright © 2011-2022 走看看