[Swift]UIImageを染める
UIImageを染める
let image:UIImage? = UIImage(named: "input_iconTest") let iv:UIImageView = UIImageView(image:image!) iv.paintColor = "ff0000" self.view.addSubview(iv)
extension UIImageView { class func getUIColorByColorHex(colorHex:String) -> UIColor{ let colorScanner:NSScanner = NSScanner(string: colorHex) var color:UInt32 = 0 colorScanner.scanHexInt(&color) let r:CGFloat = CGFloat((color & 0xFF0000) >> 16) / CGFloat(255.0); let g:CGFloat = CGFloat((color & 0x00FF00) >> 8) / CGFloat(255.0); let b:CGFloat = CGFloat(color & 0x0000FF) / CGFloat(255.0); return UIColor(red: r, green: g, blue: b, alpha: 1.0) } var paintColor:String{ get{ return "" } set{ if self.image != nil { if newValue == "" { self.image = self.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) }else{ self.tintColor = UIImageView.getUIColorByColorHex(newValue) self.image = self.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) } } } } }