2009-06-13 29 views
110

我想基于UIColor动态创建1x1 UIImage。如何动态在iPhone上创建一个彩色的1x1 UIImage?

我怀疑这可以很快用Quartz2d完成,并且我正在仔细研究试图掌握基础知识的文档。然而,它看起来像有很多潜在的缺陷:没有正确识别每个事物的位数和字节数,没有指定正确的标志,没有释放未使用的数据等。

这怎么可以用Quartz 2d(或另一种更简单的方法)?

回答

318

您可以使用CGContextSetFillColorWithColorCGContextFillRect此:

斯威夫特

extension UIImage { 
    class func image(with color: UIColor) -> UIImage { 
     let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) 
     UIGraphicsBeginImageContext(rect.size) 
     let context = UIGraphicsGetCurrentContext() 

     CGContextSetFillColorWithColor(context, color.CGColor) 
     CGContextFillRect(context, rect) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image 
    } 
} 

Swift3

extension UIImage { 
    class func image(with color: UIColor) -> UIImage { 
     let rect = CGRect(origin: CGPoint(x: 0, y:0), size: CGSize(width: 1, height: 1)) 
     UIGraphicsBeginImageContext(rect.size) 
     let context = UIGraphicsGetCurrentContext()! 

     context.setFillColor(color.cgColor) 
     context.fill(rect) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image! 
    } 
} 

Objective-C的

+ (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; 
} 
+19

不错的一个:-)我建议把这个方法作为一个类方法放到一个类中,然后它可以简单地添加到一个项目中,并且可以像`[UIImage imageWithColor:[UIColor redColor]]一样调用。 – 2012-04-08 12:19:20

-6

好吧,这不会是你想要的,但是这段代码会画出一条线。你可以调整它来表达一个观点。或者至少从中得到一些信息。

使图像1x1看起来有点奇怪。笔画骑在线上,所以在0.5的笔画宽度为1.0应该工作。只是玩耍。

- (void)drawLine{ 

UIGraphicsBeginImageContext(CGSizeMake(320,300)); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 

float x = 0; 
float xEnd = 320; 
float y = 300; 

CGContextClearRect(ctx, CGRectMake(5, 45, 320, 300)); 

CGContextSetGrayStrokeColor(ctx, 1.0, 1.0); 

CGContextSetLineWidth(ctx, 1); 
CGPoint line[2] = { CGPointMake(x,y), CGPointMake(xEnd, y) }; 

CGContextStrokeLineSegments(ctx, line, 2); 

UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 
6

我用马特·史蒂芬的回答很多次这样做了它的类别:

@interface UIImage (mxcl) 
+ (UIImage *)squareImageWithColor:(UIColor *)color dimension:(int)dimension; 
@end 

@implementation UIImage (mxcl) 
+ (UIImage *)squareImageWithColor:(UIColor *)color dimension:(int)dimension { 
    CGRect rect = CGRectMake(0, 0, dimension, dimension); 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 
@end 
7

下面是根据马特·斯蒂芬的代码的另一种选择。它会创建可调整大小的纯色图像,以便您可以重复使用它或更改其大小(例如,将其用于背景)。

+ (UIImage *)prefix_resizeableImageWithColor:(UIColor *)color { 
    CGRect rect = CGRectMake(0.0f, 0.0f, 3.0f, 3.0f); 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)]; 

    return image; 
} 

将它放入UIImage类别并更改前缀。