Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 确定UIImageView区域中透明像素的百分比_Iphone_Objective C_Ios_Uiimageview - Fatal编程技术网

Iphone 确定UIImageView区域中透明像素的百分比

Iphone 确定UIImageView区域中透明像素的百分比,iphone,objective-c,ios,uiimageview,Iphone,Objective C,Ios,Uiimageview,我正在尝试为UIImageView中定义的像素设置碰撞类型命中测试。我只希望在定义的区域中循环像素 以下是我目前掌握的情况: - (BOOL)cgHitTestForArea:(CGRect)area { BOOL hit = FALSE; CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); float areaFloat = ((area.size.width * 4) * area.size.he

我正在尝试为UIImageView中定义的像素设置碰撞类型命中测试。我只希望在定义的区域中循环像素

以下是我目前掌握的情况:

- (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;    
}
-(BOOL)cghittestforea:(CGRect)区域{
BOOL-hit=FALSE;
CGColorSpaceRef colorspace=CGColorSpaceCreateDeviceRGB();
浮动区域浮动=((面积.大小.宽度*4)*面积.大小.高度);
无符号字符*bitmapData=malloc(areaFloat);
CGContextRef context=CGBitmapContextCreate(bitmapData,
面积、大小、宽度,
面积、大小、高度,
8.
4*面积、尺寸、宽度,
色彩空间,
KCGIMAGEAlphaPremultipledLast);
CGContextTranslateCm(上下文,-area.origin.x,-area.origin.y);
[self.layer renderInContext:context];
//搜索所有像素。
浮动透明像素=0;
对于(int i=0;i<(int)areaFloat;i+=4){
//计算每个透明像素。
如果(((位图数据[i+3]*1.0)/255.0)==0){
透明像素+=1;
}
}
免费(位图数据);
//计算透明像素的百分比。
浮点命中容差=[[self.layer valueForKey:@“命中容差”]floatValue];
NSLog(@“Apixels:%f命中率:%f”,透明像素,(透明像素/areaFloat));
if((透明像素/(面积浮点/4))
有人能提供它不起作用的原因吗?

我建议使用。它允许对图像进行简单的像素级操作,而无需考虑上下文、与其他库的链接或原始内存分配。要使用视图的内容创建animgaebitmapre,可以执行以下操作:

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];
然后,您可以裁剪出所需的矩形。请注意,坐标相对于视图的左下角:

// 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:];
最后,您可以使用以下方法找到图像中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;
double totalAlpha;
双总像素;
对于(int x=0;x<[irep位图大小].x;x++){
对于(int y=0;y<[irep位图大小].y;y++){
totalAlpha+=[irep getPixelAtPoint:BMPointMake(x,y)]。alpha;
总像素数+=1;
}
}
双alphaPct=总alpha/totalPixels;
然后可以使用
alphaPct
变量作为从0到1的百分比。请注意,为了防止泄漏,必须使用release:
[irep release]
释放
ANImageBitmapRep
对象


希望我能帮忙。在iOS开发中,图像数据是一个有趣的领域

没有。这对一场比赛来说不是很好的表现。更进一步。尽管它位于完全透明的区域,但偶尔会返回一次命中。因此,我已经使用CGBitmapContextCreateImage进行了测试,以检查正在迭代哪个区域。但透明像素经常出现损坏。见图:谢谢,亚历克斯。这给了我们一些很好的方向。你对右下角坐标的评论似乎不适用于我?为什么会这样。在我展示的代码中,我放置了一些东西来将坐标从左下角转换到左上角。CGContext的工作方式是,左下角是坐标的起始位置(当谈到变换时)。我已经使用ANImageBitmapRep实现了类似于您建议的内容。但是,UIView的pointInside委托方法似乎在每次触摸时调用三次。每次命中测试创建和发布ANImageBitmapRep 3次是否低效?@markturnip是的,我会说这是低效的。也许只需要创建一个静态变量或实例变量