如何dynamic地在iPhone上创build一个彩色的1×1 UIImage?

我想创build一个基于UIColordynamic1x1 UIImage。

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

这怎么可以用Quartz 2d(或其他更简单的方法)安全地完成?

你可以使用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; } 

这是基于马特斯蒂芬的代码的另一种select。 它创build一个可resize的纯色图像,以便您可以重复使用或更改其大小(例如,将其用于背景)。

 + (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类别中,并改变前缀。

我用了很多次马特·史蒂芬的回答,所以为它做了一个分类:

 @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 

好吧,这不会是你想要的,但是这个代码会画出一条线。 你可以适应它来expression一个观点。 或者至less从中得到一些信息。

使图像1×1看起来有点奇怪。 笔画骑行,所以在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(); }