• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:04:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试检测碰撞忽略透明区域。我已经完成了所有这些,但是返回 UIImageView 中单个像素的函数是否透明。目前我从这里整理了一些帖子:

- (BOOL)IsPixelNonTransparentFromImageUIImage*)image atXint)x andYint)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;
}

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



Best Answer-推荐答案


下面是检测单个像素 alpha 的代码:

- (BOOL)IsPixelNonTransparentFromImageUIImage*)image atXint)x andYint)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap