开发工具之颜色图片相互转换

来源:互联网 发布:优化标签怎么设置 编辑:程序博客网 时间:2024/05/16 04:56

一、图片转颜色 patternImage

        self.view.backgroundColor = UIColor.init(patternImage: UIImage.init(named: "black.png")!)

二、颜色转图片

 //  颜色转换为背景图片    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    }
//  颜色转换为背景图片- (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;}
0 0
原创粉丝点击