標籤:sla ted 模式 overlay tco view圓角 圓角 send layer
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圖片原色渲染