uiimage - Invert colors of image with transparent background -
i have uiimage transparent background. if invert colors follow method, transparent background change white , black.
i want doesnt invert transparent pixels? how can that?
-(uiimage *) getimagewithinvertedpixelsofimage:(uiimage *)image { uigraphicsbeginimagecontextwithoptions(image.size, no, 2); cgcontextsetblendmode(uigraphicsgetcurrentcontext(), kcgblendmodecopy); [image drawinrect:cgrectmake(0, 0, image.size.width, image.size.height)]; cgcontextsetblendmode(uigraphicsgetcurrentcontext(), kcgblendmodedifference); cgcontextsetfillcolorwithcolor(uigraphicsgetcurrentcontext(),[uicolor whitecolor].cgcolor); cgcontextfillrect(uigraphicsgetcurrentcontext(), cgrectmake(0, 0, image.size.width, image.size.height)); uiimage * result = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return result;
}
thank you!
this how solved.
it code can find in other answers, masking of current image same image, rotated context (for transforming cg* coords ui* coords).
- (uiimage *)invertimage:(uiimage *)originalimage { uigraphicsbeginimagecontext(originalimage.size); cgcontextsetblendmode(uigraphicsgetcurrentcontext(), kcgblendmodecopy); cgrect imagerect = cgrectmake(0, 0, originalimage.size.width, originalimage.size.height); [originalimage drawinrect:imagerect]; cgcontextsetblendmode(uigraphicsgetcurrentcontext(), kcgblendmodedifference); // translate/flip graphics context (for transforming cg* coords ui* coords cgcontexttranslatectm(uigraphicsgetcurrentcontext(), 0, originalimage.size.height); cgcontextscalectm(uigraphicsgetcurrentcontext(), 1.0, -1.0); //mask image cgcontextcliptomask(uigraphicsgetcurrentcontext(), imagerect, originalimage.cgimage); cgcontextsetfillcolorwithcolor(uigraphicsgetcurrentcontext(),[uicolor whitecolor].cgcolor); cgcontextfillrect(uigraphicsgetcurrentcontext(), cgrectmake(0, 0, originalimage.size.width, originalimage.size.height)); uiimage *returnimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return returnimage; }
Comments
Post a Comment