iphone - UIImageView filter image into solid color -
i have colored icons transparent backgrounds. automatically able show flat versions of (solid color gray) depending on state of program.
could core image filters? there way?
i think i've did same thing before, have colored icon, needs keep shape , change color mono color. write extension uiimage.
here code, hope you.
uiimage+monoimage.h
// // uiimage+monoimage.h // shiftscheduler // // created zhang jiejing on 12-2-11. // copyright (c) 2012. rights reserved. // #import <uikit/uikit.h> @interface uiimage (monoimage) // function create image context , draw image mask // clip context mask // fill color user choosed. // can create image shape specify color icon. + (uiimage *) generatemonoimage: (uiimage *)icon withcolor:(uicolor *)color; @end
uiimage+monoimage.m
// // uiimage+monoimage.m // shiftscheduler // // created jiejing zhang on 12-2-11. // copyright (c) 2012. rights reserved. // #import "uiimage+monoimage.h" @implementation uiimage (monoimage) + (uiimage *) generatemonoimage: (uiimage *)icon withcolor:(uicolor *)color { uiimage *finishimage; cgimageref alphaimage = cgimageretain(icon.cgimage); cgcolorref colorref = cgcolorretain(color.cgcolor); uigraphicsbeginimagecontext(icon.size); cgcontextref ctx = uigraphicsgetcurrentcontext(); cgrect imagearea = cgrectmake (0, 0, icon.size.width, icon.size.height); // don't know why if don't translate ctm, image *bottom* // aka, head on bottom shape, need use tranlatectm , scalectm let // y-axis rotated. cgfloat height = icon.size.height; cgcontexttranslatectm(ctx, 0.0, height); cgcontextscalectm(ctx, 1.0, -1.0); cgcontextcliptomask(ctx, imagearea , alphaimage); cgcontextsetfillcolorwithcolor(ctx, colorref); cgcontextfillrect(ctx, imagearea); cgimagerelease(alphaimage); cgcolorrelease(colorref); finishimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return finishimage; } @end
Comments
Post a Comment