Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

CGImageCreate with Alpha

PitfallPitfall Posts: 7New Users
edited October 2010 in iPhone SDK Development
CGImageCreate question:

I have a block of raw pixels of data in the form of RGBA (not preMultipliedAlpha). I want to create a UIImage of that data, retaining the Alpha Channel data.

The best method I have found is to pass it through a CGImage. When I do, the CGImage has no Alpha Channel, so therefore when converted to UIImage I am without one as well.

The question is "How does one make a CGImage that contains an Alpha channel?"

//...

GLubyte *buffer = (GLubyte *) malloc(64 * 64 * 4);
// buffer contains unsigned bytes 64 X 64 X 4 bytes per pixel

//...

// make data provider from buffer
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, (64 * 64 * 4), NULL);

// set up for CGImage creation
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// test for alpha
info = CGImageGetAlphaInfo(imageRef); // Note: Returns “kCGImageAlphaNone”

// make UIImage from CGImage
UIImage *newUIImage = [UIImage imageWithCGImage:imageRef];

//...
Post edited by Pitfall on

Replies

  • PitfallPitfall Posts: 7New Users
    edited July 2009
    The flag:

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault

    Can be set up to define how Alpha is dealt with:

    CGBitmapInfo bitmapInfo =
    kCGBitmapByteOrderDefault | kCGImageAlphaLast;

    Sets it up for RGBA, non-pre-multiplied.

    Hopefully this will help someone else...


    Pitfall;104324 said:
    CGImageCreate question:

    I have a block of raw pixels of data in the form of RGBA (not preMultipliedAlpha). I want to create a UIImage of that data, retaining the Alpha Channel data.

    The best method I have found is to pass it through a CGImage. When I do, the CGImage has no Alpha Channel, so therefore when converted to UIImage I am without one as well.

    The question is "How does one make a CGImage that contains an Alpha channel?"

    //...

    GLubyte *buffer = (GLubyte *) malloc(64 * 64 * 4);
    // buffer contains unsigned bytes 64 X 64 X 4 bytes per pixel

    //...

    // make data provider from buffer
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, (64 * 64 * 4), NULL);

    // set up for CGImage creation
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    // test for alpha
    info = CGImageGetAlphaInfo(imageRef); // Note: Returns “kCGImageAlphaNone”

    // make UIImage from CGImage
    UIImage *newUIImage = [UIImage imageWithCGImage:imageRef];

    //...
  • ThaddeousThaddeous Posts: 1New Users
    edited October 2010
    THANK. YOU. This is exactly what I was looking for, and I had the EXACT same problem. I registered here just to post this :)
    Pitfall;104353 said:
    The flag:

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault

    Can be set up to define how Alpha is dealt with:

    CGBitmapInfo bitmapInfo =
    kCGBitmapByteOrderDefault | kCGImageAlphaLast;

    Sets it up for RGBA, non-pre-multiplied.

    Hopefully this will help someone else...
Sign In or Register to comment.