How do you generate an RGBA image in Core Graphics?

前端 未结 3 1169
梦谈多话
梦谈多话 2021-01-12 03:35

I\'m trying to generate an RGBA8 image from text to use as an OpenGL ES 2.0 texture.

+(UIImage *)imageFromText:(NSString *)text
{
  UIFont *font = [UIFont sy         


        
3条回答
  •  被撕碎了的回忆
    2021-01-12 04:16

    The color space you specify during creation wouldn't cause an error like that.

    The reason you're getting that error is that you've specified 8 bits per component, presumably 4 color components in the 4*size.width value you passed in for bytesPerRow, yet a bitmapInfo parameter of kCGImageAlphaNone. kCGImageAlphaNone means only RGB, not RGBA. If you want RGBA, you should most likely specify kCGImageAlphaLast kCGImageAlphaPremultipliedLast.

    [EDIT] sorry. I should have said kCGImageAlphaPremultipliedLast, not kCGImageAlphaLast.

    So, something like this:

    CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    CGContextRef contextRef =  CGBitmapContextCreate(NULL,
                                                     size.width,
                                                     size.height,
                                                     8,
                                                     4 * size.width,
                                                     colorSpace,
                                                     kCGImageAlphaPremultipliedLast);
    

提交回复
热议问题