首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGbitmapcontext和alpha的问题

CGbitmapcontext和alpha的问题
EN

Stack Overflow用户
提问于 2014-05-01 19:31:11
回答 1查看 822关注 0票数 0

我正在尝试开发一个使用核心图形的绘图应用程序。我想要的背景是阿尔法,但它是黑色的。我试过使用所有不同类型的bitmapinfo类型,但没有成功。kCGImageAlphaPremultipliedLast也不能工作。有人知道怎么解决吗?

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-01 21:11:24

我用这段代码来做你要做的事情。我把颜色设置为红色,用50%的alpha,而不是0%,这样你就可以看到alpha通道就在那里。

代码语言:javascript
复制
@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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23415095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档