gpt4 book ai didi

ios - CGImageCreate 给出完全黑色或无图像或崩溃

转载 作者:行者123 更新时间:2023-11-28 20:33:31 27 4
gpt4 key购买 nike

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

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

我相当确定我通过一些测试获得了正确的像素并将选择的颜色输出到一个 View ,该部分似乎工作正常,似乎是在我编辑原始数据后创建图像时,至少这就是我认为的问题所在。

提前为任何帮助干杯。

编辑:

我现在已经剥离了代码,所以基本上它所做的就是将 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;

最佳答案

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

此外,我还使算法的主要部分更加高效。在数组上迭代时,您总是希望将 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 ++;
}
}
}

希望对您有所帮助!

关于ios - CGImageCreate 给出完全黑色或无图像或崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11406914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com