【问题标题】:Core Graphics invert alpha核心图形反转 alpha
【发布时间】:2014-03-28 10:53:08
【问题描述】:

目前我在 Cocos2D 中遇到了 CCClippingNode 问题,并且如 here 所述的转换。现在 Viktor(SpriteBuilder 的作者)gave a tip,我可以用 Core Graphics 遮盖图像。

我现在正在做的,它有效。然而,Core Graphics 使用与 Cocos2d 完全相反的 alpha 进行遮罩。是否可以即时反转 UIImage(灰度)的 alpha 值?

如果我想用 Cocos 从图像中屏蔽一个圆圈,我需要这个蒙版。

但它必须在核心图形中如此。

是否可以即时从顶部图像创建底部图像? (例如反转它的 alpha)

【问题讨论】:

  • 如果 cocos2d v3 支持着色器,我可能会走那条路。
  • 我认为您要反转的是值(黑暗或黑度),而不是 alpha(透明度)。
  • 就是这样,谢谢!白色似乎是“不要显示这个”,黑色是“显示这个”
  • 你想用 RactImage 来用动画圈出动态图像。

标签: ios cocos2d-iphone core-graphics mask


【解决方案1】:

How to convert UIImage/CGImageRef's alpha channel to mask?

Ben 对链接问题的回答解决了给定 UIImage 的反转 alpha。只需删除/注释代码的以下部分

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
    unsigned char val = alphaData[y*strideLength + x];
    val = 255 - val;
    alphaData[y*strideLength + x] = val;
}

PS:无法添加评论。

【讨论】:

    【解决方案2】:

    我根据这个答案honors for @Tommy得到答案

    CGSize size = finalImage.size;
        int width = size.width;
        int height = size.height;
        // Create a suitable RGB+alpha bitmap context in BGRA colour space
        CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char *memoryPool = (unsigned char *)calloc(width*height*4, 1);
        CGContextRef context = CGBitmapContextCreate(memoryPool, width, height, 8, width * 4, colourSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
        CGColorSpaceRelease(colourSpace);
    
        // draw the current image to the newly created context
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), [finalImage CGImage]);
    
        // run through every pixel, a scan line at a time...
        for(int y = 0; y < height; y++)
        {
            // get a pointer to the start of this scan line
            unsigned char *linePointer = &memoryPool[y * width * 4];
    
            // step through the pixels one by one...
            for(int x = 0; x < width; x++)
            {
    
                if(linePointer[3]){
                    linePointer[3]=255-linePointer[3];
                }
                linePointer += 4;
            }
        }
    
        // get a CG image from the context, wrap that into a
        // UIImage
        CGImageRef cgImage = CGBitmapContextCreateImage(context);
        UIImage *returnImage = [UIImage imageWithCGImage:cgImage];
    
        // clean up
        CGImageRelease(cgImage);
        CGContextRelease(context);
        free(memoryPool);
    
        //use returnImage as you want
    

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 2013-07-04
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多