一段时间以来,我一直在尝试执行以下操作,
- 玩家点击屏幕
- 获取玩家点击的像素值
- 循环遍历图像以找出哪些像素是相同的
- 具有相同值的像素具有 0 的 alpha 创建一个新图像和
- 将 UIImage 设置为新创建的
- ...
- 利润
但它似乎就像标题所说的那样,完全黑屏,或者没有任何图像......可能会将所有 alpha 设置为 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 值还是由于 alpha 为 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;