【问题标题】:Transparency in CGContextCGContext 中的透明度
【发布时间】:2012-11-25 14:45:25
【问题描述】:

我正在尝试为 CGContext 设置透明背景,但不断收到:

CGBitmapContextCreateImage: invalid context 0x0

这就是我所拥有的。如果我将 kCGImageAlphaLast 切换到 kCGImageAlphaNoneSkipFirst 它可以工作,但 alpha 通道被完全忽略。我对这种颜色和上下文的东西很陌生-有什么想法吗?

-(BOOL) initContext:(CGSize)size {
int bitmapByteCount;
int bitmapBytesPerRow;

bitmapBytesPerRow = (size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * size.height);

cacheBitmap = malloc( bitmapByteCount );
if (cacheBitmap == NULL){
    return NO;
}

cacheContext = CGBitmapContextCreate (NULL, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaLast);

CGContextSetRGBFillColor(cacheContext, 1.0, 1.0, 1.0f, 0.5);
CGContextFillRect(cacheContext, self.bounds);

return YES;
}

【问题讨论】:

    标签: objective-c xcode core-graphics


    【解决方案1】:

    CGBitmapContext supports only certain possible pixel formats. 你可能想要kCGImageAlphaPremultipliedLast

    (这是explanation of premultiplied alpha。)

    另请注意:

    • 不需要malloc cacheBitmap。由于您将NULL 作为CGBitmapContextCreate 的第一个参数传递,因此位图上下文将进行自己的分配。

    • 根据您的代码,self.bounds 可能不是要填充的正确矩形。使用CGRectMake(0.f, 0.f, size.width, size.height) 会更安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 2020-07-12
      • 1970-01-01
      • 2016-10-12
      • 2013-01-19
      • 2017-01-11
      相关资源
      最近更新 更多