2

I have a Bytearray

unsigned char *outputData = (unsigned char *)malloc(sizeof(unsigned char) * w * h * 4);

    outputData[y * h * 4 + x * 4 + 0] = all; //alpha value
    outputData[y * h * 4 + x * 4 + 1] = red; //red value
    outputData[y * h * 4 + x * 4 + 2] = gre; //green value
    outputData[y * h * 4 + x * 4 + 3] = blu; //blue value

            //h... total image height
            //y,x ... current y and x value from the matrix

Now I want to convert this Data to an UIImage. How does that work? I've tried:

NSData *outdata = [NSData dataWithBytes:outputData length:sizeof(unsigned char) * w * h * 4];
UIImage *newimage = [UIImage imageWithData:outdata];

But this doesn't seem to be the right solution. Please help.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
Marco
  • 301
  • 4
  • 15
  • OK I think I solved it.. I found out that imageWithData is just creating images which have a format (png, jpeg) .. so I need to create a context first right? – Marco Jul 12 '11 at 06:55

1 Answers1

11
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmapContext=CGBitmapContextCreate(outputData, w, h, 8, 4*w, colorSpace,  kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
    CFRelease(colorSpace);
    free(outputData);
    CGImageRef cgImage=CGBitmapContextCreateImage(bitmapContext);
    CGContextRelease(bitmapContext);

    UIImage * newimage = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55