从原始RGBA数据创建UIImage [英] Creating UIImage from raw RGBA data

查看:134
本文介绍了从原始RGBA数据创建UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to convert an array RGBA data (int bytes) into a UIImage. My code looks like as follows:

/*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)。我一直试图解决这个问题,并检查了许多相关的论坛,例如(链接文字)关于如何去做它却收效甚微。

The output image that I receive is an image of width 0, and height 1080950784 (assuming my initil height and width were 240 and 240). I have been trying to get this sorted out and have checked many related forums e.g. (link text) on how to go about it but with little success.

推荐答案

事实证明,这个问题是我们俩都忽略的一个非常愚蠢的错误。 UIImage维度存储为浮点数,而不是整数。 :D

It turns out the problem is a pretty silly mistake that both of us overlooked. UIImage dimensions are stored as floats, not integers. :D

尝试

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

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

Instead. The image size has been transferred correctly.

这篇关于从原始RGBA数据创建UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆