-(uicolor *) Coloratpixel: (cgpoint) Point {
//Cancel If point is outside image coordinates
if (! Cgrectcontainspoint (cgrectmake(0.0f,0.0f, self. ) Size. width,self. size. ( height), point)) {
return Nil;
}
nsinteger pointx = trunc(point. x);
Nsinteger pointy = trunc(point. Y);
cgimageref cgimage = self. Cgimage;
nsuinteger width =self. Size. width;
nsuinteger height =self. Size. Height;
cgcolorspaceref colorspace =Cgcolorspacecreatedevicergb();
int bytesperpixel = 4;
int bytesperrow = bytesperpixel * 1;
nsuinteger bitspercomponent = 8;
unsigned Char pixeldata[4] = {0, 0, 0, 0 };
cgcontextref context = cgbitmapcontextcreate(Pixeldata,
1,
1,
Bitspercomponent,
Bytesperrow,
ColorSpace,
Kcgimagealphapremultipliedlast | kcgbitmapbyteorder32big);
cgcolorspacerelease(colorspace);
Cgcontextsetblendmode(context,kcgblendmodecopy);
//Draw The pixel we are interested in onto the bitmap context
CGCONTEXTTRANSLATECTM (Context,-pointx, pointy-(cgfloat) height);
Cgcontextdrawimage (Context, cgrectmake(0.0f, 0.0f, (cgfloat) width, (cgfloat) height), Cgimage);
cgcontextrelease(context);
//Convert color values [0..255] to floats [0.0..1.0]
cgfloat red = (cgfloat) pixeldata[0]/255.0f;
cgfloat green = (cgfloat) pixeldata[1]/255.0f;
cgfloat blue = (cgfloat) pixeldata[2]/255.0f;
cgfloat alpha = (cgfloat) pixeldata[3]/255.0f;
return [uicolorcolorwithred: Red green: Greenblue: Blue Alpha: Alpha];
}
Uiimage+coloratpixel.h #import <uikit/uikit.h>/* A category on UIImage This enables to query the color value of Arbitrary pixels of the image. */@interface UIImage (Coloratpixel)-(Uicolor *) Coloratpixel: (cgpoint) point; @end #import <coregraphics/ coregraphics.h> #import "Uiimage+coloratpixel.h" @implementation UIImage (Coloratpixel)-(Uicolor *) Coloratpixel: ( Cgpoint) point {//Cancel if point is outside image coordinates if (! Cgrectcontainspoint (CGRectMake (0.0f, 0.0f, Self.size.width, Self.size.height), point)) {return nil; } Nsinteger Pointx = Trunc (point.x); Nsinteger pointy = trunc (POINT.Y); Cgimageref cgimage = self. Cgimage; Nsuinteger width = self.size.width; Nsuinteger height = self.size.height; Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb (); int bytesperpixel = 4; int bytesperrow = Bytesperpixel * 1; Nsuinteger bitspercomponent = 8; unsigned char pixeldata[4] = {0, 0, 0, 0}; Cgcontextref context = CgbitMapcontextcreate (Pixeldata, 1, 1, Bitspercomponent, Bytesperrow, ColorSpace, Kcgimagealphapremultipliedlast | KCGBITMAPBYTEORDER32BIG); Cgcolorspacerelease (ColorSpace); Cgcontextsetblendmode (context, kcgblendmodecopy); Draw The pixel we is interested in onto the bitmap context Cgcontexttranslatectm (context,-pointx, pointy-(cgfloat) height); Cgcontextdrawimage (Context, CGRectMake (0.0f, 0.0f, (cgfloat) width, (cgfloat) height), cgimage); Cgcontextrelease (context); Convert color values [0..255] to floats [0.0..1.0] cgfloat red = (cgfloat) pixeldata[0]/255.0f; CGFloat green = (cgfloat) pixeldata[1]/255.0f; CGFloat blue = (cgfloat) pixeldata[2]/255.0f; CgfloAt alpha = (cgfloat) pixeldata[3]/255.0f; return [Uicolor colorwithred:red green:green blue:blue alpha:alpha];} @end
IOS Gets the Color object (uicolor*) of a point in the picture.