如何在iPhone上动态创建彩色1x1 UIImage?

Ian*_*ell 115 core-graphics quartz-graphics uiimage ios

我想基于UIColor动态创建1x1 UIImage.

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

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

Mat*_*ens 327

你可以使用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
    }
}
Run Code Online (Sandbox Code Playgroud)

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!
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 不错的一个:-)我建议把这个方法作为类方法放入一个类别,然后可以简单地添加到项目中,并使用像`[UIImage imageWithColor:[UIColor redColor]]`这样的行调用. (19认同)
  • 如果您在循环中创建这些图像或使用更大的尺寸,则优化将是仅在颜色具有alpha时使用不透明画布.像这样:`const CGFloat alpha = CGColorGetAlpha(color.CGColor); const BOOL opaque = alpha == 1; UIGraphicsBeginImageContextWithOptions(size,opaque,0);` (7认同)

Ben*_*man 8

这是基于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;
}
Run Code Online (Sandbox Code Playgroud)

将它放在UIImage类别中并更改前缀.


mxc*_*xcl 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
Run Code Online (Sandbox Code Playgroud)


bit*_*wit 5

使用 Apple 最新的 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))
        })
    }
}
Run Code Online (Sandbox Code Playgroud)