Question

I'm trying to develop an draw app using Core Graphics. I want to have the background to be in alpha but instead it is in black. I tried using all the different types of bitmapinfo types with no success. kCGImageAlphaPremultipliedLast will not work either. Anyone knows how to fix this?

- (BOOL) initContext:(CGSize)size {

    int bitmapByteCount;
    int bitmapBytesPerRow;

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow = (size.width * 4);
    bitmapByteCount = (bitmapBytesPerRow * size.height);

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    cacheBitmap = malloc( bitmapByteCount );
    if (cacheBitmap == NULL){
        return NO;
    }

    CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipFirst;

    cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), bitmapInfo);
    CGContextSetRGBFillColor(cacheContext, 0, 0, 0, 0);
    CGContextFillRect(cacheContext, (CGRect){CGPointZero, size});
    return YES;
}
Was it helpful?

Solution

I'm using this code to do pretty much exactly what you're asking. I've set the color to red with a 50% alpha instead of 0% so you can actually see that the alpha channel is there.

@implementation ViewController
{
    CGContextRef                    cacheContext;
    void*                           cacheBitmap;
    __weak IBOutlet UIImageView*    _imageView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self setupContext:self.view.bounds.size];

    CGImageRef      cgImage = CGBitmapContextCreateImage(cacheContext);
    _imageView.image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
}


// Name changed to avoid using the magic word "init"
- (BOOL) setupContext:(CGSize)size
{
    int bitmapByteCount;
    int bitmapBytesPerRow;

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow = (size.width * 4);
    bitmapByteCount = (bitmapBytesPerRow * size.height);

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    cacheBitmap = malloc( bitmapByteCount );
    if (cacheBitmap == NULL){
        return NO;
    }

    CGBitmapInfo    bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault;

    // Create and define the color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, colorSpace, bitmapInfo);
    CGContextSetRGBFillColor(cacheContext, 1., 0, 0, 0.5);
    CGContextFillRect(cacheContext, (CGRect){CGPointZero, size});

    // Release the color space so memory doesn't leak
    CGColorSpaceRelease(colorSpace);

    return YES;
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top