我正在嘗試爲iOS 5.1設備創建UIImage測試模式。目標UIImageView的尺寸爲320x240,但我試圖創建一個160x120 UIImage測試圖案(未來,非測試圖案圖像將是這種尺寸)。我希望盒子的上半部分是藍色的,下半部分是紅色的,但我看起來像是未初始化的內存會損壞圖像的底部。代碼如下:CGImageCreate測試模式不工作(iOS)
int width = 160;
int height = 120;
unsigned int testData[width * height];
for(int k = 0; k < (width * height)/2; k++)
testData[k] = 0xFF0000FF; // BGRA (Blue)
for(int k = (width * height)/2; k < width * height; k++)
testData[k] = 0x0000FFFF; // BGRA (Red)
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, &testData, (width * height * 4), NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
colorSpaceRef, bitmapInfo, provider, NULL, NO,renderingIntent);
UIImage *myTestImage = [UIImage imageWithCGImage:imageRef];
這應該看起來像堆棧溢出的另一個例子。無論如何,我發現當我減小測試圖案的尺寸時,圖像的「損壞」部分會增加。另外奇怪的是,我在「腐敗」部分看到了紅線,所以看起來我並沒有弄亂組件的大小。我錯過了什麼?這感覺就像提供商的東西,但我沒有看到它。
謝謝!
添加了屏幕截圖。下面是它看起來像kCGImageAlphaNoneSkipFirst集:
這裏是什麼樣子與kCGImageAlphaFirst:
啊,我敢打賭就是這樣。我應該已經意識到發生了什麼事情,但我過於專注於獲取CGx功能。我會盡量驗證今晚的答案(Windows和C/C#在工作,但Mac和iOS在家中)。謝謝! – GrandAdmiral 2012-03-26 16:49:27