例如:
基本上,给出任何图像,我不关心图像中的颜色.我想要的是创建一个表示Alpha通道的灰度图像.然后可以使用该图像来屏蔽其他图像.
当您提供图标图像时,其示例行为在UIbarbuttonItem中.根据苹果文档,它表示:
The images displayed on the bar are derived from this image. If this image is too large to fit on the bar,it is scaled to fit. Typically,the size of a toolbar and navigation bar image is 20 x 20 points. The Alpha values in the source image are used to create the images—opaque values are ignored.
UIbarbuttonItem接受任何图像,只能看到Alpha,而不是图像的颜色.
解决方法 要按条形按钮项目的方式来绘制图标,您不需要传统的面具,您想要一个面具的倒数 – 原始图像中的不透明像素占用最终着色,而不是其他方式.这是完成这个的一种方法.拿你原来的RBGA图像,并通过以下处理:
>将其绘制为仅Alpha通道位图图像
>反转每个像素的Alpha值得到与上述相反的行为
>将此倒置的Alpha图像转换为实际的掩码
>使用它
例如.
#define ROUND_UP(N,S) ((((N) + (S) - 1) / (S)) * (S))// Original RGBA imageCGImageRef originalMaskImage = [[UIImage imagenamed:@"masktest.png"] CGImage];float wIDth = CGImageGetWIDth(originalMaskImage);float height = CGImageGetHeight(originalMaskImage);// Make a bitmap context that's only 1 Alpha channel// WARNING: the bytes per row probably needs to be a multiple of 4 int strIDeLength = ROUND_UP(wIDth * 1,4);unsigned char * AlphaData = calloc(strIDeLength * height,sizeof(unsigned char));CGContextRef AlphaOnlyContext = CGBitmapContextCreate(AlphaData,wIDth,height,8,strIDeLength,NulL,kCGImageAlphaOnly);// Draw the RGBA image into the Alpha-only context.CGContextDrawImage(AlphaOnlyContext,CGRectMake(0,height),originalMaskImage);// Walk the pixels and invert the Alpha value. This lets you colorize the opaque shapes in the original image.// If you want to do a Traditional mask (where the opaque values block) just get rID of these loops.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; }}CGImageRef AlphaMaskImage = CGBitmapContextCreateImage(AlphaOnlyContext);CGContextRelease(AlphaOnlyContext);free(AlphaData);// Make a maskCGImageRef finalMaskImage = CGImageMaskCreate(CGImageGetWIDth(AlphaMaskImage),CGImageGetHeight(AlphaMaskImage),CGImageGetBitsPerComponent(AlphaMaskImage),CGImageGetBitsPerPixel(AlphaMaskImage),CGImageGetBytesPerRow(AlphaMaskImage),CGImageGetDataProvIDer(AlphaMaskImage),false);CGImageRelease(AlphaMaskImage);
现在,您可以使用finalMaskImage作为CGContextClipToMask等中的掩码等.
总结以上是内存溢出为你收集整理的ios – 如何将UIImage / CGImageRef的Alpha通道转换为掩码?全部内容,希望文章能够帮你解决ios – 如何将UIImage / CGImageRef的Alpha通道转换为掩码?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)