Programmatically create image from UILabel

前端 未结 3 1578
谎友^
谎友^ 2021-01-02 20:48

I want to use a UILabel to create an RGBA stream of bytes image representation programmatically (at runtime).

For example, I want to create a UILabel in a specific

3条回答
  •  無奈伤痛
    2021-01-02 21:42

    CGFloat scale  = [[[view window] screen] scale];
    CGRect  bounds = [view bounds];
    
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(NULL, CGRectGetWidth(bounds) * scale, CGRectGetHeight(bounds) * scale, 8, CGRectGetWidth(bounds) *
    scale * 4, space, kCGImageAlphaPremultipliedLast);
    CGContextTranslateCTM(ctx, 0.0, CGRectGetHeight(bounds) * scale);
    CGContextScaleCTM(ctx, scale, -scale);
    [[view layer] renderInContext:ctx];
    

    Then, use the bytes via CGBitmapContextGetData(myContext). No reason to bother with a UIImage

提交回复
热议问题