IOS Gets the Color object (uicolor*) of a point in the picture.

Source: Internet
Author: User




-(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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.