4

i need help converting an 24/32 bit RGB raw image to uiimage.

I've tried the examples here from Paul Solt and others, but nothing work. Anybody could please show an example or tutorial?

The image data is hold in nsdata and i would like to have a jpg or png image.

Thx Thorsten

I'm using the code by Paul Solt, it does something, but the image looks like it have four times the image information in one image. i cant post an image here: EDIT: i added the lines at the beginning of the method between the comments, now it works :-)

+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer
                                withWidth:(int) width
                               withHeight:(int) height {

    // added code
    char* rgba = (char*)malloc(width*height*4);
    for(int i=0; i < width*height; ++i) {
        rgba[4*i] = buffer[3*i];
        rgba[4*i+1] = buffer[3*i+1];
        rgba[4*i+2] = buffer[3*i+2];
        rgba[4*i+3] = 255;
    }
    // 


    size_t bufferLength = width * height * 4;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgba, bufferLength, NULL);
    size_t bitsPerComponent = 8;
    size_t bitsPerPixel = 32;
    size_t bytesPerRow = 4 * width;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    if(colorSpaceRef == NULL) {
        NSLog(@"Error allocating color space");
        CGDataProviderRelease(provider);
        return nil;
    }

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGImageRef iref = CGImageCreate(width,
                                    height,
                                    bitsPerComponent,
                                    bitsPerPixel,
                                    bytesPerRow,
                                    colorSpaceRef,
                                    bitmapInfo,
                                    provider,   // data provider
                                    NULL,       // decode
                                    YES,            // should interpolate
                                    renderingIntent);

    uint32_t* pixels = (uint32_t*)malloc(bufferLength);

    if(pixels == NULL) {
        NSLog(@"Error: Memory not allocated for bitmap");
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpaceRef);
        CGImageRelease(iref);
        return nil;
    }

    CGContextRef context = CGBitmapContextCreate(pixels,
                                                 width,
                                                 height,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpaceRef,
                                                 bitmapInfo);

    if(context == NULL) {
        NSLog(@"Error context not created");
        free(pixels);
    }

    UIImage *image = nil;
    if(context) {

        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);

        CGImageRef imageRef = CGBitmapContextCreateImage(context);

        // Support both iPad 3.2 and iPhone 4 Retina displays with the correct scale
        if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
            float scale = [[UIScreen mainScreen] scale];
            image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
        } else {
            image = [UIImage imageWithCGImage:imageRef];
        }

        CGImageRelease(imageRef);   
        CGContextRelease(context);  
    }

    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(iref);
    CGDataProviderRelease(provider);

    if(pixels) {
        free(pixels);
    }   
    return image;
}
thorb65
  • 2,696
  • 2
  • 27
  • 37
  • Post some code... what you have tried pls – Arun Jul 16 '13 at 10:50
  • Look at once http://stackoverflow.com/questions/8704577/is-it-possible-to-make-a-nsbitmapimagerep-with-24-bpp-and-no-alpha-channel?rq=1 – Arun Jul 16 '13 at 10:54
  • what exactly do you mean by raw data? Do you mean manufacturer specific camera RAW or do you just mean RGB bitmap data? – Hermann Klecker Jul 16 '13 at 10:58
  • I#m trying to connect to a scanner. the sanner sends "preview" data in "color RGB raw data". thats all informations i have. – thorb65 Jul 16 '13 at 11:30
  • http://stackoverflow.com/questions/1579631/converting-rgb-data-into-a-bitmap-in-objective-c-cocoa check this it may help you – Divyam shukla Jul 16 '13 at 12:34
  • @DivZ... the firest lines which adds the alpha does the magic :-) thx – thorb65 Jul 16 '13 at 12:51

2 Answers2

4

solution:

my bitmap image data has only 3 bytes, but ios wants 4 bytes, the fourth is for alpha. so inserting the following code which added a 4th byte fixed the problem.

char* rgba = (char*)malloc(width*height*4);
for(int i=0; i < width*height; ++i) {
    rgba[4*i] = buffer[3*i];
    rgba[4*i+1] = buffer[3*i+1];
    rgba[4*i+2] = buffer[3*i+2];
    rgba[4*i+3] = 255;
}
thorb65
  • 2,696
  • 2
  • 27
  • 37
-2

Here is Conversion of NSData to UIImage :

NSData *imageData = UIImagePNGRepresentation(image);

UIImage *image=[UIImage imageWithData:data];

I have created PNG image using this code, hope it will works for you also.

sagarcool89
  • 1,366
  • 8
  • 16
  • Sorry, but no it doesn't. As i wrote, i have no png representation. The data is raw rgb 24/32bit. but thx :-) – thorb65 Jul 16 '13 at 11:20