iphone - How to get the pixel color on touch? -


i know common question , there lot of answers of question. i've used of this. although many of them same. sad thing me none of them worked me. following codes i've used till now.

-(void)getrgbasfromimage:(uiimage*)image atx:(int)xx andy:(int)yy {     // first image data buffer     cgimageref imageref = [image cgimage];     nsuinteger width = cgimagegetwidth(imageref);     nsuinteger height = cgimagegetheight(imageref);     cgcolorspaceref colorspace = cgcolorspacecreatedevicergb();     unsigned char *rawdata = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));     nsuinteger bytesperpixel = 4;     nsuinteger bytesperrow = bytesperpixel * width;     nsuinteger bitspercomponent = 8;     cgcontextref context = cgbitmapcontextcreate(rawdata, width, height,                                                  bitspercomponent, bytesperrow, colorspace,                                                  kcgimagealphapremultipliedlast | kcgbitmapbyteorder32big);     cgcolorspacerelease(colorspace);      cgcontextdrawimage(context, cgrectmake(0, 0, width, height), imageref);     cgcontextrelease(context);      // rawdata contains image data in rgba8888 pixel format.     int byteindex = (bytesperrow * yy) + xx * bytesperpixel;      cgfloat red   = (rawdata[byteindex]     * 1.0) / 255.0;     cgfloat green = (rawdata[byteindex + 1] * 1.0) / 255.0;     cgfloat blue  = (rawdata[byteindex + 2] * 1.0) / 255.0;     cgfloat alpha = (rawdata[byteindex + 3] * 1.0) / 255.0;     byteindex += 4;     nslog(@"the vale of rbg of red %f",red);      democolor.tintcolor = [uicolor colorwithred:red green:green blue:blue alpha:alpha];     free(rawdata); } 

here approach i've used -

- (cgcontextref) createargbbitmapcontextfromimage:(cgimageref) inimage {      cgcontextref    context = null;     cgcolorspaceref colorspace;     void *          bitmapdata;     int             bitmapbytecount;     int             bitmapbytesperrow;      // image width, height. we'll use entire image.     size_t pixelswide = cgimagegetwidth(inimage);     size_t pixelshigh = cgimagegetheight(inimage);      // declare number of bytes per row. each pixel in bitmap in     // example represented 4 bytes; 8 bits each of red, green, blue, ,     // alpha.     bitmapbytesperrow   = (pixelswide * 4);     bitmapbytecount     = (bitmapbytesperrow * pixelshigh);      // use generic rgb color space.     //colorspace = cgcolorspacecreatewithname(kcgcolorspacegenericrgb);     colorspace = cgcolorspacecreatedevicergb();     if (colorspace == null)     {         fprintf(stderr, "error allocating color space\n");         return null;     }      // allocate memory image data. destination in memory     // drawing bitmap context rendered.     bitmapdata = malloc( bitmapbytecount );     if (bitmapdata == null)     {         fprintf (stderr, "memory not allocated!");         cgcolorspacerelease( colorspace );         return null;     }      // create bitmap context. want pre-multiplied argb, 8-bits     // per component. regardless of source image format     // (cmyk, grayscale, , on) converted on format     // specified here cgbitmapcontextcreate.     context = cgbitmapcontextcreate (bitmapdata,                                      pixelswide,                                      pixelshigh,                                      8,      // bits per component                                      bitmapbytesperrow,                                      colorspace,                                      kcgimagealphapremultipliedfirst);     if (context == null)     {         free (bitmapdata);         fprintf (stderr, "context not created!");     }      // make sure , release colorspace before returning     cgcolorspacerelease( colorspace );      return context; }   - (uicolor*) getpixelcoloratlocation:(cgpoint)point {     uicolor* color = nil;     //cgimageref inimage = self.image.cgimage;     cgimageref inimage = [appdelegate getinstance].capturedimage.cgimage;     // create off screen bitmap context draw image into. format argb 4 bytes each pixel: alpa, red, green, blue     cgcontextref cgctx = [self createargbbitmapcontextfromimage:inimage];     if (cgctx == null) { return nil; /* error */ }      size_t w = cgimagegetwidth(inimage);     size_t h = cgimagegetheight(inimage);     cgrect rect = {{0,0},{w,h}};       // draw image bitmap context. once draw, memory     // allocated context rendering contain     // raw image data in specified color space.     cgcontextdrawimage(cgctx, rect, inimage);       // can pointer image data associated bitmap     // context.     unsigned char* data = cgbitmapcontextgetdata (cgctx);     if (data != null) {         //offset locates pixel in data x,y.         //4 4 bytes of data per pixel, w width of 1 row of data.         int offset = 4*((w*round(point.y))+round(point.x));         int alpha =  data[offset];         int red = data[offset+1];         int green = data[offset+2];         int blue = data[offset+3];         nslog(@"offset: %i colors: rgb %i %i %i  %i",offset,red,green,blue,alpha);         color = [uicolor colorwithred:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];     }      // when finished, release context     cgcontextrelease(cgctx);     // free image data memory context     if (data) { free(data); }      return color; } 

but none of these has been worked me. please me out work this. there i'm missing ?

i have 2 uiimageview in ui. 1 in contains image need pick color of pixel touched. , other uiimageview paint on image picked color.

please help. appreciated.

this 1 i've used, , looks simpler methods you've tried.

in custom view class, have this:

- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event {     uitouch *touch = [[event alltouches] anyobject];     cgpoint loc = [touch locationinview:self];     self.pickedcolor = [self colorofpoint:loc]; } 

colorofpoint method in category on uiview, code:

#import "uiview+colorofpoint.h" #import <quartzcore/quartzcore.h>  @implementation uiview (colorofpoint)  -(uicolor *) colorofpoint:(cgpoint)point     {     unsigned char pixel[4] = {0};     cgcolorspaceref colorspace = cgcolorspacecreatedevicergb();     cgcontextref context = cgbitmapcontextcreate(pixel,             1, 1, 8, 4, colorspace, (cgbitmapinfo)kcgimagealphapremultipliedlast);      cgcontexttranslatectm(context, -point.x, -point.y);      [self.layer renderincontext:context];      cgcontextrelease(context);     cgcolorspacerelease(colorspace);     uicolor *color = [uicolor colorwithred:pixel[0]/255.0         green:pixel[1]/255.0 blue:pixel[2]/255.0         alpha:pixel[3]/255.0];     return color;     } 

don't forget import category custom view class , add quartzcore framework.


trivial note 2013: cast last argument (cgbitmapinfo) avoid implicit conversion warning: example here. hope helps.


Comments