gpt4 book ai didi

ios - 访问 UIImageView 中的单个像素 alpha

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:35 25 4
gpt4 key购买 nike

我正在尝试忽略透明区域来检测碰撞。我已经完成了所有这些,但是返回 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) 被“绘制”到我们创建的位图上下文的单个像素中。这样就避免了动态分配和渲染整张图片来加快处理速度。 kCGImageAlphaOnly 用于仅捕获图片的 alpha channel 。

这是一个测试这段代码的程序:

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] ---------************---------

关于ios - 访问 UIImageView 中的单个像素 alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com