Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:03:01+00:00 2026-05-26T00:03:01+00:00

This is the Case i have an unsigned char pointer to BMP image data

  • 0

This is the Case
i have an unsigned char pointer to BMP image data
after i loop with the pointer i achieved a byte array contain int values 0 – 255

What i want is convert this values in the array to BMP image
to display it in UIImage.

**the image is in gray scale

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:03:02+00:00Added an answer on May 26, 2026 at 12:03 am

    This code snippet is from this blog, I recommend you take a look there and the project site in Github

    Also Note that this class method works for RGB8 Images, so you will need to make changes in
    bitsPerPixel (it should be 8 for grayscale), bytesPerRow (1 * width), bufferLength (remove * 4) and create colorSpaceRef using CGColorCreateGenericGray instead.

    Also I’ve noticed that creating the color space with CGColorCreateGenericGray assumes that your array has alpha info. So maybe you should add an alpha byte for each pixel to make it work properly.

    + (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer 
                withWidth:(int) width
               withHeight:(int) height {
    
    
        size_t bufferLength = width * height * 4;
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array as follows, unsigned char A[16] I am using this array
Dynamic bitset I have a use case where i need to populate boost::dynamic_bitset<unsigned char>
I have a char array that is really used as a byte array and
I have an unsigned char* data with the following values, as seen while debugging
I have a regular .NET application. In this case I have a part that's
Imagine this case where I have an object that I need to check a
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I have an object (in this case a rating object from js-kit) that I
Scenario: I have a document I created using LaTeX (my resume in this case),
Suppose that I have a Java program within an IDE (Eclipse in this case).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.