當我試圖用像素陣列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