iOS开发,用UIColor创建UIImage

邰英毅
2023-12-01

+(UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {

    

    @autoreleasepool {

        

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

        UIGraphicsBeginImageContext(rect.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, color.CGColor);

        CGContextFillRect(context, rect);

        

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

        

        UIGraphicsEndImageContext();

        

        return img;

    }

}

 类似资料: