Swift Image Color rendering

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.