gpt4 book ai didi

iphone - 获取UIImage的像素颜色

转载 作者:行者123 更新时间:2023-12-03 18:09:51 24 4
gpt4 key购买 nike

如何获取 UIImage 中特定像素的 RGB 值?

最佳答案

尝试这个非常简单的代码:

我曾经在迷宫游戏中检测墙壁(我需要的唯一信息是 Alpha channel ,但我包含了为您获取其他颜色的代码):

- (BOOL)isWallPixel:(UIImage *)image xCoordinate:(int)x yCoordinate:(int)y {

CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
const UInt8* data = CFDataGetBytePtr(pixelData);

int pixelInfo = ((image.size.width * y) + x ) * 4; // The image is png

//UInt8 red = data[pixelInfo]; // If you need this info, enable it
//UInt8 green = data[(pixelInfo + 1)]; // If you need this info, enable it
//UInt8 blue = data[pixelInfo + 2]; // If you need this info, enable it
UInt8 alpha = data[pixelInfo + 3]; // I need only this info for my maze game
CFRelease(pixelData);

//UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f]; // The pixel color info

if (alpha) return YES;
else return NO;

}

关于iphone - 获取UIImage的像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3284185/

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