ios - iPhone : How to change color of particular pixel of a UIImage? -
i have uiimage, want change colour of particular pixel, 224 x 200th pixel. how can this? possible do? in advance.
after searching lot complete day got exact solution, though found many none of them worked. here have got, works flawlessly -:
// method creates image changing individual pixels of image. color of pixel has been taken array of colours('avgrgbsofpixel') -(void)createtexture{ self.sampleimage = [[uiimageview alloc]initwithimage:[uiimage imagenamed:@"whitebg.jpg"]]; cgrect imagerect = cgrectmake(0, 0, self.sampleimage.image.size.width, self.sampleimage.image.size.height); uigraphicsbeginimagecontext(sampleimage.image.size); cgcontextref context = uigraphicsgetcurrentcontext(); //save current status of graphics context cgcontextsavegstate(context); cgcontextdrawimage(context, imagerect, sampleimage.image.cgimage); // , draw point on wherever want this: // cgcontextfillrect(context, cgrectmake(x,y,1,1)); //fix error according @gsempe's comment int pixelcount =0; for(int = 0 ; i<sampleimage.image.size.width ; i++){ for(int j = 0 ; j<sampleimage.image.size.height ; j++){ pixelcount++; int index = pixelcount/pixelcountforcalculatingavgcolor; // nslog(@"index : %d", index); if(index >= [avgrgbsofpixel count]){ index = [avgrgbsofpixel count] -1; nslog(@"bad index"); } // if(pixelcount >9999){ // pixelcount = 9999; // nslog(@"bad index"); // } uicolor *color = [avgrgbsofpixel objectatindex:index]; float red ,green, blue ,alpha ; [color getred:&red green:&green blue:&blue alpha:&alpha]; cgcontextsetrgbfillcolor(context, red , green, blue , 1); cgcontextfillrect(context, cgrectmake(i,j,1,1)); } } // save uiimage again: cgcontextrestoregstate(context); uiimage *img = uigraphicsgetimagefromcurrentimagecontext(); [self.iv setimage:img];
}
Comments
Post a Comment