我能夠找到一種方法來幫助我修改應用程序中UIImage中像素的alpha值,但是我遇到了兩個錯誤(第二個肯定是肯定的由第一個引起)。然而,我不能弄清楚發生了什麼。iPhone SDK - <Error>:CGBitmapContextCreate:不受支持的顏色空間
我的方法:
- (void)modifyAlpha:(int)x and:(int)y {
CGContextRef ctx;
CGImageRef imageRef = [scratchOffImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
rawData[byteIndex+3] = (char) (255); //Change the pixel value
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8,
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
kCGImageAlphaPremultipliedFirst); //This line causes an error: incorrect colorspace
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
scratchOffImage = rawImage;
[scratchOffImageView setImage:scratchOffImage];
free(rawData);
}
錯誤:
<Error>: CGBitmapContextCreate: unsupported color space.
正在上線拋出:
ctx = CGBitmapContextCreate(rawData, ...
然後第二錯誤:
<Error>: CGBitmapContextCreateImage: invalid context 0x0
被拋出就行了:
imageRef = CGBitmapContextCreateImage (ctx);
我的形象被包含在捆綁我最初是由它輸出Photoshop的保存,使用PNG-8 transparancy作爲格式Web功能做到了。
當我運行該函數首次使用的示例代碼時,示例圖像正常工作。但是,我的圖像沒有。我怎樣才能調試呢?
有沒有人有任何想法我可以調試呢?可能我的輸入PNG格式不正確?我怎樣才能檢查這個?
乾杯, 佈雷特
EDIT 1:原始的源代碼來從例如發現here。該示例顯示轉換爲灰度,而我只需要更改alpha值。編輯2:我試過把我的圖像保存爲PNG-8和PNG-24,兩者都沒有運氣。
CGImageGetWidth(imageRef);和CGImageGetHeight(imageRef);如果在調試器剛剛設置後停止,則返回圖像的有效值? – 2012-01-09 19:51:30
@Joachim:確實如此。這是我檢查的第一件事情之一。 :-) – Brett 2012-01-09 19:52:29