检索UIImage的像素alpha值

检索UIImage的像素alpha值

我目前正在尝试获取UIImageView中像素的alpha值。我从[UIImageView image]中获取了CGImage,并从中创建了一个RGBA字节数组。Alpha是预乘的。


CGImageRef image = uiImage.CGImage;

NSUInteger width = CGImageGetWidth(image);

NSUInteger height = CGImageGetHeight(image);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

rawData = malloc(height * width * 4);

bytesPerPixel = 4;

bytesPerRow = bytesPerPixel * width;


NSUInteger bitsPerComponent = 8;

CGContextRef context = CGBitmapContextCreate(

    rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace,

    kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big

);

CGColorSpaceRelease(colorSpace);


CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);

CGContextRelease(context);

然后,我使用UIImageView中的坐标计算给定alpha通道的数组索引。


int byteIndex = (bytesPerRow * uiViewPoint.y) + uiViewPoint.x * bytesPerPixel;

unsigned char alpha = rawData[byteIndex + 3];

但是我没有得到我期望的价值。对于图像的完全黑色透明区域,我得到alpha通道的非零值。我是否需要翻译UIKit和Core Graphics之间的坐标 - 即:y轴是倒置的吗?或者我误解了预乘alpha值?


更新:


@Nikolai Ruhe的建议是关键。我实际上并不需要在UIKit坐标和Core Graphics坐标之间进行转换。但是,在设置混合模式后,我的alpha值就是我的预期:


CGContextSetBlendMode(context, kCGBlendModeCopy);


阿波罗的战车
浏览 830回答 3
3回答

呼如林

如果你想要的只是单个点的alpha值,你只需要一个只有alpha的单点缓冲区。我相信这应该足够了:// assume im is a UIImage, point is the CGPoint to testCGImageRef cgim = im.CGImage;unsigned char pixel[1] = {0};CGContextRef context = CGBitmapContextCreate(pixel,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1, 1, 8, 1, NULL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kCGImageAlphaOnly);CGContextDrawImage(context, CGRectMake(-point.x,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-point.y,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CGImageGetWidth(cgim),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CGImageGetHeight(cgim)),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cgim);CGContextRelease(context);CGFloat alpha = pixel[0]/255.0;BOOL transparent = alpha < 0.01;如果不必每次都重新创建UIImage,这非常有效。编辑2011年12月8日:一位意见提供者指出,在某些情况下,图像可能会被翻转。我一直在考虑这个,我有点遗憾,我没有直接使用UIImage编写代码,就像这样(我认为原因是当时我不明白UIGraphicsPushContext):// assume im is a UIImage, point is the CGPoint to testunsigned char pixel[1] = {0};CGContextRef context = CGBitmapContextCreate(pixel,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1, 1, 8, 1, NULL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kCGImageAlphaOnly);UIGraphicsPushContext(context);[im drawAtPoint:CGPointMake(-point.x, -point.y)];UIGraphicsPopContext();CGContextRelease(context);CGFloat alpha = pixel[0]/255.0;BOOL transparent = alpha < 0.01;我认为这样可以解决翻转问题。

倚天杖

我是否需要翻译UIKit和Core Graphics之间的坐标 - 即:y轴是倒置的吗?这是可能的。在CGImage中,像素数据采用英语阅读顺序:从左到右,从上到下。所以,阵列中的第一个像素是左上角;&nbsp;第二个像素是从顶行左侧开始的一个像素;&nbsp;等等假设你有这个权利,你还应该确保你在一个像素内查看正确的组件。也许你期待RGBA但要求ARGB,反之亦然。或者,也许您的字节顺序错误(我不知道iPhone的字节顺序是什么)。或者我误解了预乘alpha值?听起来不像。对于那些不知道的人:预乘意味着颜色分量被alpha预乘;&nbsp;无论颜色分量是否被预乘,alpha分量都是相同的。您可以通过将颜色分量除以alpha来反转此(非预乘)。
打开App,查看更多内容
随时随地看视频慕课网APP