CGImageCreate完全黑色或没有图像或崩溃

时间:2012-07-10 05:04:39

标签: ios cgimageref cgbitmapcontextcreate

我已经有一段时间试图做以下事了,

  • 玩家点击屏幕
  • 获取玩家点击的像素值
  • 循环显示图像,找出哪些像素相同。全部
  • 具有相同值的像素的alpha为0,创建新图像和
  • 将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;

2 个答案:

答案 0 :(得分: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 ++;
        }
    }
}

希望它有所帮助!

答案 1 :(得分: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);”显然记忆不喜欢被释放。