Swift圖片原色渲染

來源:互聯網
上載者:User

標籤: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圖片原色渲染

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.