Getting RGB of a pixel in touchesMoved delegate -
i have created page picks rgb values of pixel. allows user move finger , choose pixel color of selected image. setting rgb hence obtained, small image view showing selection.
here piece of code.
- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [[event alltouches] anyobject]; // touch location cgpoint touchlocation = [touch locationinview:touch.view]; // set touch location's center imageview if (cgrectcontainspoint(imageviewcolorpicker.frame, touchlocation)) { imageviewcolorpicker.center = touchlocation; cgimageref image = [imageviewselectedimage.image cgimage]; nsuinteger width = cgimagegetwidth(image); nsuinteger height = cgimagegetheight(image); cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); unsigned char *rawdata = malloc(height * width * 4); 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),image); cgcontextrelease(context); int byteindex = (bytesperrow * (int)touchlocation.y) + (int)touchlocation.x * bytesperpixel; red = rawdata[byteindex]; green = rawdata[byteindex + 1]; blue = rawdata[byteindex + 2]; alpha = rawdata[byteindex + 3]; imgpixelcolor.backgroundcolor=[uicolor colorwithred:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha]; } }
it solving problem. issue crashes during finger move showing received memory warning
message 3 times in log window.
am doing wrong? there other way rgb's solve kind of problem? library if available ?
quick appreciated.
you're malloc
ing pixel buffer graphics context (rawdata
), never free
it, you're leaking copy of entire image, every time touch moves (this can lot of memory pretty quickly).
add @ end of if-statement.
free(rawdata);
btw, may want consider creating bitmap context once , reusing it. it's pretty wasteful redraw image every time touch moves, if stays same anyway.
Comments
Post a Comment