How to create a colored 1x1 UIImage on the iPhone dynamically?

我希望基于 UIColor 动态创建一个1x1的 UIImage。

I suspect this can quickly be done with Quartz2d, and I'm poring over the documentation trying to get a grasp of the fundamentals. However, it looks like there are a lot of potential pitfalls: not identifying the numbers of bits and bytes per things correctly, not specifying the right flags, not releasing unused data, etc.

如何使用 Quartz 2d (或其他更简单的方法)安全地完成这项工作?

38417 次浏览

Ok, this won't be exactly what you want, but this code will draw a line. You can adapt it to make a point. Or at least get a little info from it.

把图像做成1x1看起来有点奇怪。行程乘以直线,因此宽度为1.0的行程在0.5应该可以工作。随便玩玩。

- (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();
}

你可以使用 CGContextSetFillColorWithColorCGContextFillRect:

Swift

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!
}
}

目标 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;
}

I used Matt Steven's answer many times so made a category for it:

@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

这是另一个基于 Matt Stephen 代码的选项。它创建一个可调整大小的纯色图像,这样你就可以重复使用它或者改变它的大小(例如用它作为背景)。

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

To me, a convenience init feels neater in Swift.

extension UIImage {


convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {


let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}


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


guard let image = context.makeImage() else {
return nil
}
UIGraphicsEndImageContext()


self.init(cgImage: image)
}


}

使用苹果最新的 UIGraphicsImageRenderer,代码非常小:

import UIKit


extension UIImage {
static func from(color: UIColor) -> UIImage {
let size = CGSize(width: 1, height: 1)
return UIGraphicsImageRenderer(size: size).image(actions: { (context) in
context.cgContext.setFillColor(color.cgColor)
context.fill(.init(origin: .zero, size: size))
})
}
}