获取UIImage的Pixel颜色

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



元芳怎么了
浏览 466回答 3
3回答

慕哥9229398

您无法直接访问原始数据,但通过获取此图像的CGImage,您可以访问它。这里是另一个问题的链接,可以回答您的问题以及您可能拥有的有关详细图像处理的其他问题:CGImage

holdtom

试试这个非常简单的代码:我曾经在我的迷宫游戏中检测到墙壁(我需要的唯一信息是alpha通道,但我包含了代码以获取其他颜色):- (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;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS