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

后端 未结 6 2037
情书的邮戳
情书的邮戳 2020-11-28 01:53

I would like to create a 1x1 UIImage dynamically based on a UIColor.

I suspect this can quickly be done with Quartz2d, and I\'m poring over the documentation trying

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 02:23

    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.

    Making the image 1x1 seems a little weird. Strokes ride the line, so a stroke of width 1.0 at 0.5 should work. Just play around.

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

提交回复
热议问题