iOS开发-UIColor和UIImage之间的转换

UIColor 转UIImage(可将该方法作为一个UIImage的分类)

+(UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size;

{

        //描述一个矩形

        CGRect rect = CGRectMake(0.0f, 0.0f,size.width,size.height);

        //开启图形上下文

        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

        //获得图形上下文

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        //使用color演示填充上下文

        CGContextSetFillColorWithColor(ctx, [color CGColor]);

        //渲染上下文

        CGContextFillRect(ctx, rect);

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        //关闭图形上下文

        UIGraphicsEndImageContext();

        return image;

}

UIImage转UIColor

[UIColor colorWithPatternImage:[UIImage imageNamed:@"EmailBackground.png"]]

原文地址:https://www.cnblogs.com/guitarandcode/p/5758931.html