Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

iphone - Creating UIImage from raw RGBA data

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;

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Try

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

Instead. The image size has been transferred correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...