访问 UIImageView 中的单个像素 alpha

Accesing a single pixels alpha in an UIImageView

我正在尝试忽略透明区域来检测碰撞。我已经完成了所有这些,但是 returns 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;
}

这不仅对我不起作用,而且我很确定有一个更圆滑的解决方案来找到一个像素。任何帮助将不胜感激。

这里是应该检测单个像素的 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) 处的点进入 "drawn"我们创建的位图上下文的单个像素。这样就避免了动态分配和渲染整张图片来加快处理速度。 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] ---------************---------