zoukankan      html  css  js  c++  java
  • xcode 产生指定颜色的图片imageWithColor

      是在万能的stackOverflow上找到的答案,留下了,

    原地址:http://stackoverflow.com/questions/6496441/creating-a-uiimage-from-a-uicolor-to-use-as-a-background-image-for-uibutton

    通过颜色产生图片其实还是蛮常用的,以下先贴出大神的OC代码

    + (UIImage *)imageWithColor:(UIColor *)color
    {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }

    下面贴出小弟的Swift代码:

        func imageWithColor(color: UIColor) -> UIImage{
            var rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
            UIGraphicsBeginImageContext(rect.size)
            var context = UIGraphicsGetCurrentContext()
            
            CGContextSetFillColorWithColor(context, color.CGColor)
            CGContextFillRect(context, rect)
            
            var image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }

    只用将代码拷贝到类中直接调用就OK了,嘎方便的。

  • 相关阅读:
    命名空间
    XML
    关于命名空间
    gitbook 入门教程之 gitbook 简介
    git 入门教程之备忘录[译]
    git 入门教程之知识速查
    git 入门教程之忽略文件
    git 入门教程之个性化 git
    git 入门教程之里程碑式标签
    git 入门教程之本地和远程仓库的本质
  • 原文地址:https://www.cnblogs.com/scaptain/p/3975238.html
Copyright © 2011-2022 走看看