gpt4 book ai didi

iphone - 从原始 RGBA 数据创建 UIImage

转载 作者:行者123 更新时间:2023-12-03 18:24:52 29 4
gpt4 key购买 nike

我一直在尝试将数组 RGBA 数据(int 字节)转换为 UIImage。我的代码如下所示:

/*height and width are integers denoting the dimensions of the image*/
unsigned char *rawData = malloc(width*height*4);

for (int i=0; i<width*height; ++i)
{
rawData[4*i] = <red_val>;
rawData[4*i+1] = <green_val>;
rawData[4*i+2] = <blue_val>;
rawData[4*i+3] = 255;
}


/*I Have the correct values displayed
- ensuring the rawData is well populated*/

NSLog(@"(%i,%i,%i,%f)",rawData[0],rawData[1],rawData[2],rawData[3]/255.0f);
NSLog(@"(%i,%i,%i,%f)",rawData[4],rawData[5],rawData[6],rawData[7]/255.0f);
NSLog(@"(%i,%i,%i,%f)",rawData[8],rawData[9],rawData[10],rawData[11]/255.0f);



CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
rawData,
width*height*4,
NULL);

int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4*width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width,
height,
8,
32,
4*width,colorSpaceRef,
bitmapInfo,
provider,NULL,NO,renderingIntent);
/*I get the current dimensions displayed here */
NSLog(@"width=%i, height: %i", CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef) );
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
/*This is where the problem lies.
The width, height displayed are of completely different dimensions
viz. the width is always zero and the height is a very huge number */

NSLog(@"resultImg width:%i, height:%i",
newImage.size.width,newImage.size.height);


return newImage;

我收到的输出图像是宽度为 0、高度为 1080950784 的图像(假设我的初始高度和宽度分别为 240 和 240)。我一直在努力解决这个问题,并检查了许多相关论坛,例如(link text)关于如何去做但收效甚微。

最佳答案

事实证明,这个问题是一个非常愚蠢的错误,我们都忽略了。 UIImage 尺寸存储为 float ,而不是整数。 :D

尝试

NSLog(@"resultImg width:%f, height:%f",
newImage.size.width,newImage.size.height);

相反。图像尺寸已正确传输。

关于iphone - 从原始 RGBA 数据创建 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4545237/

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