this question exact duplicate of:
using extension in swift. changing overlay color of uiimage
results in grainy/pixelated image.
extension uiimage{ func tint(color: uicolor) -> uiimage{ uigraphicsbeginimagecontext(self.size) let context = uigraphicsgetcurrentcontext() // below 2 lines, scaling & translating doesn't have affect. // read in post add fix doesn't work context!.scaleby(x: 1.0, y: -1.0) context!.translateby(x: 0.0, y: -self.size.height) context!.setblendmode(.multiply) let rect = cgrect(x: 0, y: 0, width: self.size.width, height: self.size.height) context!.clip(to: rect, mask: self.cgimage!) color.setfill() context!.fill(rect) let newimage = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return newimage! } }
what doing wrong?
is there another/better approach?
solutions in objective-c welcomed.
you can use uigraphicsbeginimagecontextwithoptions
.
this function lets pass in scale factor apply image third parameter. if specify 0.0
value, it'll automatically set main screen's scale (which useful retina displays).
in case call be
uigraphicsbeginimagecontextwithoptions(self.size, false, 0.0)
the second argument specifies whether image's alpha channel should ignored or not.
Comments
Post a Comment