iphone - How do I desaturate a UIImage? -
is there easy way (or built-in library) in ios 5.x desaturate uiimage? here's how doing it:
cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsavegstate(context); cgcontexttranslatectm(context, 0.0, self.bounds.size.height); // flip image right side cgcontextscalectm(context, 1.0, -1.0); cgcontextdrawimage(context, rect, self.image.cgimage); cgcontextsetblendmode(context, kcgblendmodesaturation); cgcontextcliptomask(context, self.bounds, image.cgimage); // restricts drawing within alpha channel cgcontextsetrgbfillcolor(context, 0.0, 0.0, 0.0, desaturation); cgcontextfillrect(context, rect); cgcontextrestoregstate(context); // restore state reset blend mode this seems bit more complicated expected. there simpler way this?
i thinking of core image, can't seem find concrete example on how desaturate image using that.
you can open source gpuimage framework using 2 or 3 lines of code:
uiimage *inputimage = [uiimage imagenamed:@"inputimage.png"]; gpuimagegrayscalefilter *grayscalefilter = [[gpuimagegrayscalefilter alloc] init]; uiimage *grayscaleimage = [grayscalefilter imagebyfilteringimage:inputimage]; (remembering release filter if not building using arc)
this reduces image luminance values, desaturating it. if want variable saturation / desaturation, can use gpuimagesaturationfilter. framework name indicates, filtering runs on gpu, , faster core image in every situation i've benchmarked on ios of 5.1.
Comments
Post a Comment