Swift图片原色渲染
Posted word_hard(be_nice)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift图片原色渲染相关的知识,希望对你有一定的参考价值。
public class HuaUtilitityImage: NSObject {
// 普通渲染
public func renderingImageWithTintColor(image: UIImage?,_ tintColor: UIColor) -> UIImage?{
return renderingImageWithThintColor(image, tintColor, CGBlendMode.DestinationIn)
}
// 渐变渲染
public func renderingImageWithGradientThintColor(image: UIImage?,_ tintColor: UIColor) -> UIImage?{
return renderingImageWithThintColor(image, tintColor, CGBlendMode.Overlay)
}
// 渲染(图片UIImage,渲染颜色,渲染模式)
public func renderingImageWithThintColor(image: UIImage?,_ tintColor: UIColor,_ blendMode: CGBlendMode) -> UIImage?{
if image == nil{
return nil
}
UIGraphicsBeginImageContextWithOptions(image!.size, false, 0.0)//初始化绘图上下文[绘图大小,不透明性,缩放比]
tintColor.setFill()//设置填充颜色到绘图上下文
let bounds = CGRectMake(0, 0, image!.size.width, image!.size.height)//绘图边界
UIRectFill(bounds)//填充颜色
image!.drawInRect(bounds, blendMode: blendMode, alpha: 1.0)//绘制图片
if blendMode != CGBlendMode.DestinationIn {//渐变绘制
image!.drawInRect(bounds, blendMode: CGBlendMode.DestinationIn, alpha: 1.0)
}
let tintedImage = UIGraphicsGetImageFromCurrentImageContext()//取回上下文图片
UIGraphicsEndImageContext()//结束绘图上下文
return tintedImage
}
// 渲染后的图片还有可能被系统上下文渲染器自动渲染,所以要根据不同情况进行是否取消系统自动渲染
public func translateRenderingModeImage(image:UIImage?,_ renderingMode:UIImageRenderingMode) -> UIImage?{
if image == nil{
return nil
}
//UIImageRenderingMode.Automatic 设置根据系统上下文自动判断是否被系统渲染,可能原色/可能被渲染[默认]
//UIImageRenderingMode.AlwaysOriginal 一直保持原来的颜色,不被系统自动渲染[不作为渲染模板]
//UIImageRenderingMode.AlwaysTemplate 一直保持为渲染模板色被渲染
return image?.imageWithRenderingMode(renderingMode)
}
// 普通渲染原色模式
public func renderingImageForNavigation(image: UIImage,_ color:UIColor) -> UIImage{
return translateRenderingModeImage(renderingImageWithTintColor(image, color), UIImageRenderingMode.AlwaysOriginal)!
}
// 设置UIImageView圆角
public func setImageViewCornerRadius(imageView:UIImageView?, radio:Int){
imageView?.layer.cornerRadius = CGFloat(radio)
imageView?.layer.masksToBounds = true
}
}
以下是简单地调用:
@IBOutlet weak var imageRending: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "bookImage")
imageRending.image = HuaUtilitityImage().renderingImageForNavigation(image!, UIColor.greenColor())
}
以上是关于Swift图片原色渲染的主要内容,如果未能解决你的问题,请参考以下文章