我已經有一段時間了試圖做到以下幾點,CGImageCreate給出完全地黑色或無圖像或崩潰
- 玩家水龍頭屏幕
- 讓玩家挖掘的像素值
- 遍歷圖像,找出哪些像素都一樣使所有
- 像素具有相同的值是0的阿爾法創建一個新的形象和
- 設置的UIImage爲新創建的一個
- ...
- 利潤
但它要麼似乎做正如標題所說,全黑的屏幕,或沒有任何圖像......所有的α-可能可能設置爲0,或崩潰的東西有關堆棧上的壞訪問該功能CGSConvertBGRX8888toRGBA8888
這裏附近是函數
+ (UIImage*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy
{
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
NSUInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
NSUInteger bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
NSUInteger bytesPerRow = CGImageGetBytesPerRow(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) malloc(height * width * 4);
CGContextRef context = CGBitmapContextCreate(rawData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextClearRect(context, CGRectMake(0, 0, width, height));
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
//convert phone screen coords to texture coordinates.
xx *= width/320;
yy *= height/480;
int counter = 0;
// Now your rawData contains the image data in the RGBA8888 pixel format.
// Get the byteIndex of pixel tapped.
int byteIndex = (bytesPerRow * yy) + xx * bitsPerPixel;
CGFloat red = (rawData[byteIndex] * 1.0)/255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0)/255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0)/255.0;
byteIndex += 4;
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
byteIndex = (width * y) + x * bitsPerPixel;
CGFloat redVal = (rawData[byteIndex] * 1.0)/255.0;
CGFloat greenVal = (rawData[byteIndex + 1] * 1.0)/255.0;
CGFloat blueVal = (rawData[byteIndex + 2] * 1.0)/255.0;
CGFloat alphaVal = (rawData[byteIndex + 3] * 1.0)/255.0;
byteIndex += 4;
if(alphaVal != 0)
{
if(redVal == red && greenVal == green && blueVal == blue)
{
rawData[byteIndex + 3] = 0;
counter ++;
}
}
}
}
NSLog(@"Pixels amount: %i", counter);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
rawData,
width*height*4,
NULL);
CGImageRef newCGimage = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpace,
kCGBitmapByteOrderDefault,
provider,
NULL, NO, kCGRenderingIntentDefault);
UIImage *newImage = [UIImage imageWithCGImage:newCGimage];
CGDataProviderRelease(provider);
CGImageRelease(newCGimage);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(rawData);
return newImage;
}
我相當肯定我正在獲得通過一些測試正確的像素和輸出選擇一種觀點認爲部分似乎做工精細的色彩,這似乎是當我在創建後的圖像編輯原始數據,至少這是我認爲問題所在。
提前爲任何幫助而歡呼。
編輯:
我現在已經剝離了代碼,所以本質上它是所有複製PNG原始數據從它創建一個UIImage,並將其設置,基本上我應該得到完全相同的圖像回來,沒有什麼應該改變。但是這一次它只是自敗,無論是從一切0值或由於阿爾法是0我不知道編輯的代碼如下
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
NSUInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
NSUInteger bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
NSUInteger bytesPerRow = CGImageGetBytesPerRow(imageRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
CGBitmapInfo imageInfo = CGImageGetBitmapInfo(imageRef);
unsigned char *rawData = (unsigned char*) malloc(height * width * 4);
CGContextRef context = CGBitmapContextCreate(rawData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
imageInfo);
//convert phone screen coords to texture coordinates.
xx *= (width/[[UIScreen mainScreen] bounds].size.width);
yy *= (height/[[UIScreen mainScreen] bounds].size.height);
NSLog(@"converted X pos: %i",xx);
NSLog(@"converted Y pos: %i",yy);
int counter = 0;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
rawData,
width*height*4,
NULL);
CGImageRef newCGimage = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpace,
imageInfo,
provider,
NULL, NO, kCGRenderingIntentDefault);
UIImage *newImage = [[UIImage alloc]initWithCGImage:newCGimage];
CGDataProviderRelease(provider);
CGImageRelease(newCGimage);
CGContextRelease(context);
free(rawData);
return newImage;