0

一段时间以来,我一直在尝试执行以下操作,

  • 玩家点击屏幕
  • 获取玩家点击的像素值
  • 循环遍历图像以找出哪些像素是相同的
  • 具有相同值的像素具有 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;
4

2 回答 2

2

在您的编辑帖子中,图像全黑,因为您没有对上下文做任何事情。您只需创建一个空白上下文并将其转换为图像。

另外,我让你的算法的主要部分更有效率。在遍历数组时,您总是希望将 for 循环按行优先顺序(在这种情况下,外部循环用于高度,然后内部循环用于宽度),以便索引计算更容易。

int byteIndex = (bytesPerRow * yy) + (xx * (bitsPerPixel / 8));  //you want bytes, not bits, so divide the bitsPerPixel by 8

//you don't need to do the "* 1.0 / 255.0" part if you are just
//going to compare the values with other values in the same 0-255 range.
//Only do it if something requires the values to be from 0-1 or if that
//is how you want it to print.
CGFloat red   = (rawData[byteIndex++];
CGFloat green = (rawData[byteIndex++];
CGFloat blue  = (rawData[byteIndex++];
CGFloat alpha = (rawData[byteIndex++];

//because the 2-dimensional data is in a 1-dimensional array,
//the lines wrap as they go down, so the data reads like a book
//with height = 0 at the top and width = 0 on the left.
//This for loop configuration allows for easy iteration by just
//incrementing the index while iterating.

byteIndex = 0;

for (int y = 0; y < height; y++)
{
    for(int x = 0; x < width; x++) 
    {
        //Incrementing byteIndex like this makes it go to the correct
        //position for the next loop cycle.

        //Do not multiply by one unless you want maximum readability.
        //If you were trying to get out of doing integer division,
        //multiplying by one isn't necessary because the 255.0 not an integer.
        //Because multiplying by one is executed at run-time, it hogs
        //the CPU for a little bit. To overcome this, cast if you really
        //have to, because casting is a compile time operation,
        //not a run-time one, giving more performance.

        CGFloat redVal   = rawData[byteIndex++];
        CGFloat greenVal = rawData[byteIndex++];
        CGFloat blueVal  = rawData[byteIndex++];
        CGFloat alphaVal = rawData[byteIndex++];

        //you can combine the if statements into one because of short-circuiting

        if( alphaVal != 0 && redVal == red && greenVal == green && blueVal == blue )
        {
            rawData[byteIndex-1] = 0; 
            counter ++;
        }
    }
}

希望能帮助到你!

于 2013-08-22T23:53:24.527 回答
1

好吧,我已经修复了它,按照其他示例有时只会让您花费一整天的时间试图找出您不想做的事情......无论哪种方式,上面的代码都很好......除了

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 ++;
        }
    }
}

在我的像素检查中再次使用它之前,我正在增加字节索引,所以在测试之后移动它,这一切都很好。

至于图片显示问题,去掉“free(rawData);” 显然内存不喜欢被释放。

于 2012-07-11T02:38:12.053 回答