可能重复: How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)? Get Pixel color of UIImage
我有一个场景,用户可以从图像中选择一种颜色,例如下面的颜色:
根据用户在图像上的点击位置,我需要在该点提取RGB和alpha值,或者说像素。我该如何做到这一点?
您需要从图像创建位图上下文(CGContextRef
)并将点击的CGPoint
转换为数组偏移位置,以从像素数据中检索颜色信息。
请参阅What Color is My Pixel?获取教程和此similar Stack Overflow question。
方法:
- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage
返回表示使用正确颜色空间作为参数传递的图像的
CGContextRef
。此方法称为:
- (UIColor*) getPixelColorAtLocation:(CGPoint) point
这是你要通过
UIColor
获得CGPoint
的功能。
请注意,这些方法位于UIImageView
的子类中,以使该过程更加简单。