核心图形反转透明度

4

目前我在Cocos2D和过渡中遇到了CCClippingNode的问题,如这里所述。现在Viktor(SpriteBuilder的作者)给出了一个提示,说我可以使用Core Graphics来掩蔽图像。

我现在正在做这个,并且它是有效的。然而,Core Graphics使用的遮罩透明度与Cocos2d完全相反。是否有可能在运行时倒转UIImage(灰度)的Alpha值?

如果我想用Cocos裁剪出一个圆形图像,我需要这个遮罩。

Cocos Mask

但是它需要在核心图形中变成这样。

Core Graphics

是否有可能在运行时从顶部图像创建底部图像?(例如反转其Alpha值)


也许这个链接有帮助:https://dev59.com/GUvSa4cB1Zd3GeqPivqH - CodeSmile
如果cocos2d v3支持着色器,我可能会选择这条路。 - YvesLeBorg
1
我认为你想要反转的是值(暗度或黑度水平),而不是 alpha 值(透明度水平)。 - RobP
就是这样,谢谢!白色似乎表示“不显示此内容”,黑色则表示“显示此内容”。 - Matthijn
您想使用动画将ReactImage转换为圆形图片。 - Kirit Modi
2个回答

0

我根据这个答案得到了答案 @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

0

如何将UIImage/CGImageRef的alpha通道转换为掩码?

链接问题中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:无法添加评论。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接