代码之家  ›  专栏  ›  技术社区  ›  Ian Terrell

如何在iPhone上动态创建彩色1x1 ui图像?

  •  112
  • Ian Terrell  · 技术社区  · 16 年前

    我想基于uicolor动态创建一个1x1 ui图像。

    我怀疑这可以用quartz2d很快完成,我正在仔细研究文档,试图掌握基本原理。然而,看起来有很多潜在的陷阱:没有正确地识别每件事物的位和字节数,没有指定正确的标志,没有释放未使用的数据,等等。

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

    6 回复  |  直到 7 年前
        1
  •  325
  •   Wanbok Choi    8 年前

    你可以使用 CGContextSetFillColorWithColor CGContextFillRect 为此:

    迅捷

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

    Objtovi-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;
    }
    
        2
  •  8
  •   Ben Lachman    11 年前

    这是另一个基于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类别并更改前缀。

        3
  •  6
  •   mxcl    12 年前

    我用了马特·史蒂文的答案很多次,所以把它列为一个类别:

    @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
    
        4
  •  2
  •   bitwit Julio Gorgé    7 年前

    使用苹果最新的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))
            })
        }
    }
    
        5
  •  0
  •   Mark Bridges    7 年前

    对我来说,一个便利店在迅速的时候感觉更整洁。

    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)
        }
    
    }
    
        6
  •  -6
  •   Corey Floyd    16 年前

    好吧,这不是你想要的,但这段代码会画一条线。你可以调整它来说明问题。或者至少从中获得一些信息。

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