public class Huautilitityimage:nsobject {
Normal rendering
public func Renderingimagewithtintcolor (image:uiimage?,_ tintcolor:uicolor), UIImage? {
return Renderingimagewiththintcolor (image, Tintcolor, Cgblendmode.destinationin)
}
Gradient rendering
public func Renderingimagewithgradientthintcolor (image:uiimage?,_ tintcolor:uicolor), UIImage? {
return Renderingimagewiththintcolor (image, Tintcolor, Cgblendmode.overlay)
}
Rendering (picture uiimage, rendering color, rendering mode)
public func renderingimagewiththintcolor (image:uiimage?,_ tintcolor:uicolor,_ blendmode:cgblendmode), UIImage? {
if image = = nil{
return Nil
}
Uigraphicsbeginimagecontextwithoptions (image!. Size, False, 0.0)//Initialize drawing context [drawing size, opacity, zoom ratio]
Tintcolor.setfill ()//Set Fill color to drawing context
let bounds = CGRectMake (0, 0, image!. Size.width, image!. Size.Height)//Draw boundary
Uirectfill (bounds)//Fill Color
image!. Drawinrect (Bounds, Blendmode:blendmode, alpha:1.0)//drawing pictures
If Blendmode! = cgblendmode.destinationin {//gradient Draw
image!. Drawinrect (Bounds, blendMode:CGBlendMode.DestinationIn, alpha:1.0)
}
Let Tintedimage = Uigraphicsgetimagefromcurrentimagecontext ()//Retrieve context Picture
Uigraphicsendimagecontext ()//End Drawing context
Return Tintedimage
}
The rendered image may also be rendered automatically by the system context renderer, so whether or not to cancel automatic rendering of the system according to different circumstances
public func translaterenderingmodeimage (image:uiimage?,_ renderingmode:uiimagerenderingmode), UIImage? {
if image = = nil{
return Nil
}
Uiimagerenderingmode.automatic settings are automatically determined by the system context to be rendered by the system, possibly primary colors/may be rendered [default]
Uiimagerenderingmode.alwaysoriginal always retains its original color and is not automatically rendered by the system [not as a rendering template]
Uiimagerenderingmode.alwaystemplate has been rendered as render template color
return image?. Imagewithrenderingmode (Renderingmode)
}
Normal rendering primary color mode
public func renderingimagefornavigation (image:uiimage,_ color:uicolor), uiimage{
Return Translaterenderingmodeimage (renderingimagewithtintcolor (image, color), uiimagerenderingmode.alwaysoriginal )!
}
Set Uiimageview Fillet
public func Setimageviewcornerradius (Imageview:uiimageview, Radio:int) {
ImageView?. Layer.cornerradius = CGFloat (radio)
ImageView?. Layer.maskstobounds = True
}
}
The following is simply called:
@IBOutlet weak var imagerending:uiimageview!
Override Func Viewdidload () {
Super.viewdidload ()
Let image = UIImage (named: "Bookimage")
Imagerending.image = Huautilitityimage (). Renderingimagefornavigation (image!, Uicolor.greencolor ())
}
Swift Image Color rendering