[英]Identify a percentage of transparent pixels in an area of UIImageView
I'm trying to set up a collision type hit test for a defined of pixels within a UIImageView.我正在尝试为 UIImageView 中定义的像素设置碰撞类型命中测试。 I'm only wish to cycle through pixels in a defined area.我只希望循环通过定义区域中的像素。
Here's what I have so far:这是我到目前为止所拥有的:
- (BOOL)cgHitTestForArea:(CGRect)area {
BOOL hit = FALSE;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
float areaFloat = ((area.size.width * 4) * area.size.height);
unsigned char *bitmapData = malloc(areaFloat);
CGContextRef context = CGBitmapContextCreate(bitmapData,
area.size.width,
area.size.height,
8,
4*area.size.width,
colorspace,
kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -area.origin.x, -area.origin.y);
[self.layer renderInContext:context];
//Seek through all pixels.
float transparentPixels = 0;
for (int i = 0; i < (int)areaFloat ; i += 4) {
//Count each transparent pixel.
if (((bitmapData[i + 3] * 1.0) / 255.0) == 0) {
transparentPixels += 1;
}
}
free(bitmapData);
//Calculate the percentage of transparent pixels.
float hitTolerance = [[self.layer valueForKey:@"hitTolerance"]floatValue];
NSLog(@"Apixels: %f hitPercent: %f",transparentPixels,(transparentPixels/areaFloat));
if ((transparentPixels/(areaFloat/4)) < hitTolerance) {
hit = TRUE;
}
CGColorSpaceRelease(colorspace);
CGContextRelease(context);
return hit;
}
Is someone able to offer any reason why it isn't working?有人能够提供它不起作用的任何理由吗?
I would suggest using ANImageBitmapRep .我建议使用ANImageBitmapRep 。 It allows for easy pixel-level manipulation of images without the hassle of context, linking against other libraries, or raw memory allocation.它允许对图像进行简单的像素级操作,而无需麻烦的上下文、链接到其他库或原始 memory 分配。 To create an ANImgaeBitmapRep with the contents of a view, you could do something like this:要使用视图的内容创建 ANImgaeBitmapRep,您可以执行以下操作:
BMPoint sizePt = BMPointMake((int)self.frame.size.width,
(int)self.frame.size.height);
ANImageBitmapRep * irep = [[ANImageBitmapRep alloc] initWithSize:sizePt];
CGContextRef ctx = [irep context];
[self.layer renderInContext:context];
[irep setNeedsUpdate:YES];
Then, you can crop out your desired rectangle.然后,您可以裁剪出您想要的矩形。 Note that coordinates are relative to the bottom left corner of the view:请注意,坐标是相对于视图的左下角的:
// assuming aFrame is our frame
CGRect cFrame = CGRectMake(aFrame.origin.x,
self.frame.size.height - (aFrame.origin.y + aFrame.size.height),
aFrame.size.width, aFrame.size.height);
[irep cropFrame:];
Finally, you can find the percentage of alpha in the image using the following:最后,您可以使用以下方法找到图像中 alpha 的百分比:
double totalAlpha;
double totalPixels;
for (int x = 0; x < [irep bitmapSize].x; x++) {
for (int y = 0; y < [irep bitmapSize].y; y++) {
totalAlpha += [irep getPixelAtPoint:BMPointMake(x, y)].alpha;
totalPixels += 1;
}
}
double alphaPct = totalAlpha / totalPixels;
You can then use the alphaPct
variable as a percentage from 0 to 1. Note that, to prevent leaks, you must release the ANImageBitmapRep
object using release: [irep release]
.然后,您可以使用alphaPct
变量作为 0 到 1 的百分比。请注意,为防止泄漏,您必须使用 release: [irep release]
释放ANImageBitmapRep
object。
Hope that I helped.希望我有所帮助。 Image data is a fun and interesting field when it comes to iOS development.在 iOS 开发中,图像数据是一个有趣且有趣的领域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.