当我试图用像素阵列CoreGraphics中绘制的图像在我的像素阵列忽略。阿尔法使用CGBitmapContextCreate
我的形象是568x320这里是我的代码:
int k = 0;
unsigned char pixels[320 * 568*4];
for(int i = 0; i<568/1;i++) {
for(int j = 0; j<320/1;j++) {
pixels[k] = 212; //B
pixels[k+1] = 2; //G
pixels[k+2] = 87;//R
pixels[k+3] = drand48()*255; //A
k+=4;
}
}
void *baseAddress = &pixels;
NSUInteger bitsPerComponent = 8;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(baseAddress, 568, 320, 8, 4*568, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *textureImage = [UIImage imageWithCGImage:cgImage];
基本上,颜色看起来正确的,但没有透明度。如果alpha为0,则图像为黑色,否则为蓝色。
任何想法,为什么我越来越没有透明度?
编辑:
这里是我的结果,我和阿尔法
想到我买了
for(int i = 0; i<568/1;i++) {
for(int j = 0; j<320/1;j++) {
[[UIColor colorWithRed:87./255 green:2.0/255 blue:212.2/255 alpha:drand48()] setFill];
CGContextFillRect(ctx, CGRectMake(i, j, 1, 1));
}
}
第二个结果的结果,但相比它的极其缓慢到CGBitmapContextCreate
您是否尝试过“正常”的设置与RGB秩序和单独'kCGImageAlphaPremultipliedLast'标志? – Kegluneq
你是指什么正常设置?去ARGB而不是BGRA? – Neva
我的意思是rgba(因此'kCGImageAlphaPremultipliedLast'标志)。 – Kegluneq