在ImageView中访问单个像素alpha

时间:2015-04-15 21:05:03

标签: ios objective-c uiimageview

我试图通过忽略透明区域来检测碰撞。我已经完成了所有这一切,但是在UIImageView中返回或不返回单个像素的函数是透明的还是不透明的。目前我在这里汇总了一些帖子:

- (BOOL)IsPixelNonTransparentFromImage:(UIImage*)image atX:(int)x andY:(int)y {
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger 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), imageRef);
    CGContextRelease(context);
    NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
    CGFloat alpha = (rawData[byteIndex + 3] * 1.0)/255.0;
    free(rawData);
    return alpha > 0.0;
}

这不仅对我不起作用,而且我非常确定只有找到一个像素才能找到更加流畅的解决方案。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

以下是应该检测单个像素的alpha的代码:

- (BOOL)IsPixelNonTransparentFromImage:(UIImage*)image atX:(int)x andY:(int)y {
    unsigned char pixel[1] = { 0 };
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 1, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
    UIGraphicsPushContext(context);
    [image drawAtPoint:CGPointMake(-x, -y)];
    UIGraphicsPopContext();
    CGContextRelease(context);
    return pixel[0] != 0;
}

我们的想法是将图像绘制到偏移量为(-x, -y)的一个像素一个像素的上下文中,以便(x, y)处的点得到"绘制"进入我们创建的位图上下文的单个像素。这避免了动态分配和渲染整个画面以加速处理。 kCGImageAlphaOnly用于仅捕获图片的Alpha通道。

以下是测试此代码的程序:

UIImage *image = [UIImage imageNamed:@"BlueCircle.png"];
NSLog(@"%@", [NSValue valueWithCGSize:image.size]);
for (int y = 0 ; y < image.size.height ; y++) {
    NSMutableString *buf = [NSMutableString string];
    for (int x = 0 ; x < image.size.width ; x++) {
        [buf appendFormat:@"%c", [self IsPixelNonTransparentFromImage:image atX:x andY:y] ? '*' : '-'];
    }
    NSLog(@"%@", buf);
}

这是link to the BlueCircle.png image that I used for testing

当此代码运行时,我会在日志中获得此打印输出:

2015-04-15 20:15:55.213[10456:620425] ---------************---------
2015-04-15 20:15:55.213[10456:620425] -------****************-------
2015-04-15 20:15:55.214[10456:620425] ------******************------
2015-04-15 20:15:55.214[10456:620425] ----**********************----
2015-04-15 20:15:55.215[10456:620425] ---************************---
2015-04-15 20:15:55.216[10456:620425] ---************************---
2015-04-15 20:15:55.216[10456:620425] --**************************--
2015-04-15 20:15:55.217[10456:620425] -****************************-
2015-04-15 20:15:55.217[10456:620425] -****************************-
2015-04-15 20:15:55.218[10456:620425] ******************************
2015-04-15 20:15:55.219[10456:620425] ******************************
2015-04-15 20:15:55.219[10456:620425] ******************************
2015-04-15 20:15:55.220[10456:620425] ******************************
2015-04-15 20:15:55.278[10456:620425] ******************************
2015-04-15 20:15:55.279[10456:620425] ******************************
2015-04-15 20:15:55.279[10456:620425] ******************************
2015-04-15 20:15:55.280[10456:620425] ******************************
2015-04-15 20:15:55.280[10456:620425] ******************************
2015-04-15 20:15:55.281[10456:620425] ******************************
2015-04-15 20:15:55.281[10456:620425] ******************************
2015-04-15 20:15:55.282[10456:620425] ******************************
2015-04-15 20:15:55.282[10456:620425] -****************************-
2015-04-15 20:15:55.283[10456:620425] -****************************-
2015-04-15 20:15:55.283[10456:620425] --**************************--
2015-04-15 20:15:55.284[10456:620425] ---************************---
2015-04-15 20:15:55.284[10456:620425] ---************************---
2015-04-15 20:15:55.330[10456:620425] ----**********************----
2015-04-15 20:15:55.331[10456:620425] ------******************------
2015-04-15 20:15:55.331[10456:620425] -------****************-------
2015-04-15 20:15:55.332[10456:620425] ---------************---------