Byte Array to UIImage Objective-C

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:

  • How to change color of letters of sectionIndexTitlesForTableView?
  • iOS7 / iOS6 Conditional Rotation Portrait / Landscape for different sections of App
  • How to create popover in iPhone app?
  • Best practices for iOS applications security
  • UINavigationController popViewControllerAnimated: crash in iOS 6
  • How to build project with unsupported class (new feature) in xcode and swift
  • 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.

    Solutions Collect From Internet About “Byte Array to UIImage Objective-C”

        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);